如何在JSON.NET中使用实际值而不是{$ ref:XX}来序列化对象

时间:2014-01-27 00:03:43

标签: json.net

我正在使用WebAPI。我的配置看起来像这样

        var json = config.Formatters.JsonFormatter;
        json.MaxDepth = 1;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
        json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

        config.Formatters.Remove(config.Formatters.XmlFormatter);

仅显示第一项,其余实例显示为DegreeYear:{$ ref:3} 我想显示所有这些,我允许我显示的设置/配置。我是 只对第一级感兴趣,如果Degree Year有其他子对象我不想要它们。

Items: [{$id:2, OrgSchlDgExmSchdlID_PK:20, OrgSchlDgID_FK:3, DegreeYearID_FK:5, ExamName:1ST Term,…},…]
0: {$id:2, OrgSchlDgExmSchdlID_PK:20, OrgSchlDgID_FK:3, DegreeYearID_FK:5, ExamName:1ST Term,…}
$id: "2"
DegreeYear: {$id:3, DegreeYearID_PK:5, DegreeID_FK:7, Code:1YR, YearNumber:1, Description:1 YR, RowStatusID_FK:1,…}
DegreeYearID_FK: 5
Description: "XXXXXXXXXXXXXXXXXX"
ExamMonth: "April"
ExamName: "1ST Term"
OrgSchlDgExmSchdlID_PK: 20
OrgSchlDgID_FK: 3
RowStatu: null
RowStatusID_FK: 1
1: {$id:4, OrgSchlDgExmSchdlID_PK:22, OrgSchlDgID_FK:3, DegreeYearID_FK:5, ExamName:2ND Term,…}
$id: "4"
DegreeYear: {$ref:3}
DegreeYearID_FK: 5
Description: "YYYYYYYYYYYYYYYYYYYYYYYYY"
ExamMonth: "July"
ExamName: "2ND Term"
OrgSchlDgExmSchdlID_PK: 22
OrgSchlDgID_FK: 3
RowStatu: null
RowStatusID_FK: 1
2: {$id:5, OrgSchlDgExmSchdlID_PK:23, OrgSchlDgID_FK:3, DegreeYearID_FK:5, ExamName:Final, ExamMonth:NOV,…}
$id: "5"
DegreeYear: {$ref:3}
DegreeYearID_FK: 5
Description: "ZZZZZZZZZZZZZZZZZZZZZZ"
ExamMonth: "NOV"
ExamName: "Final"
OrgSchlDgExmSchdlID_PK: 23
OrgSchlDgID_FK: 3
RowStatu: null
RowStatusID_FK: 1

1 个答案:

答案 0 :(得分:0)

只需在格式化程序配置中删除该行

即可
 [formatter].SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;