JSON.NET:如何序列化嵌套集合
这让我疯了……我正在使用Json.net将List序列化为JSON。 我期待这个JSON:
{ "fieldsets": [ { "properties": [ { "alias": "date", "value": "2014-02-12T00:00:00" }, { "alias": "time", "value": null } ], "alias": "eventDates", "disabled": false } ] }
但相反,我得到了这个:
{ "fieldsets": [ { "properties": [ { "values": [ { "alias": "date", "value": "2014-07-13T00:00:00" }, { "alias": "time", "value": "Registration begins at 8:00 AM; walk begins at 9:00 AM" } ] } ], "alias": "eventDates", "disabled": false } ] }
我想要的“值”集合只是一个JSON数组,但我不能为我的生活弄清楚如何让它做到这一点。 我在“属性”对象上有一个名为“values”的属性,所以我理解它为什么这样做,但我只需要直接数组,而不是JSON对象。
对于该响应,您需要此类结构
public class Property { [JsonProperty("alias")] public string Alias { get; set; } [JsonProperty("value")] public string Value { get; set; } } public class Fieldset { [JsonProperty("properties")] public Property[] Properties { get; set; } [JsonProperty("alias")] public string Alias { get; set; } [JsonProperty("disabled")] public bool Disabled { get; set; } } public class Response { [JsonProperty("fieldsets")] public Fieldset[] Fieldsets { get; set; } }
这可能是答案:
public class Property { public string alias { get; set; } public string value { get; set; } } public class Fieldset { public List properties { get; set; } public string alias { get; set; } public bool disabled { get; set; } } public class RootObject { public List