如何反序列化此Json并保存SampleClass列表中的所有数据字段和 HeaderText
public class SampleClass
{
public string Datafield { get; set; }
public string HeaderText { get; set; }
}
{
"SummaryResults": {
"ControlType": "Table",
"ColumnDefinitions": [
{
"Datafield": "SourceSystem",
"HeaderText": "Source System"
},
{
"Datafield": "ChildHoldType",
"HeaderText": "Child Hold Type"
},
{
"Datafield": "CompanyNumber",
"HeaderText": "Company Number"
},
{
"Datafield": "Queue",
"HeaderText": "Queue"
},
{
"Datafield": "Country",
"HeaderText": "Country"
},
{
"HeaderText": "Count",
"ControlDefinition": {
"ControlType": "Hyperlink",
"DisplayText": "@{Count}",
"URL" : ""
}
}
]
}
}
答案 0 :(得分:0)
使用Json.Net的LINQ-to-JSON API很简单:
List<SampleClass> list = JToken.Parse(json)
.SelectToken("SummaryResults.ColumnDefinitions")
.Children<JObject>()
.Select(jo => jo.ToObject<SampleClass>())
.ToList();
但是,请注意,JSON数组中的最后一项没有Datafield
,因此在结果列表中它将为null。