我有JSON数据,如下所示:
{
"position":[
{
"top":[
42,
12
]
},
{
"middle":[
10,
15
]
},
{
"bottom":[
5,
201
]
},
{
"axis":[
{
"X":[
901,
51,
67
]
},
{
"Y":[
1474186,
561647
]
},
{
"Z":[
911,
1296501
]
},
15,
20
]
}
],
"validated":true,
"metadata":{
"uri":"/data/complex/",
"session":[
1818,
14
],
"private":false
},
"vists":0,
"ok":true,
"data":{
"10":{
"title":"Alpha",
"author":"Justin X. Ample",
"cover":"/f48hf58.tiff"
},
"901":{
"title":"Tau",
"author":"Felina Blank",
"cover":"/45trthrref.tiff"
}
},
"live":null
}
Run Code Online (Sandbox Code Playgroud)
从这些数据我想显示如下列表:
Alpha, Justin X. Ample
Tau, Felina Blank
Run Code Online (Sandbox Code Playgroud)
请注意,键(在我的示例中为10和901)是不可预测的.所以我想以某种方式创建一个表示"数据"结构的对象,并迭代它以获得每个条目的标题和作者.
使用基本的JSON结构,我成功完成了这样的事情(使用JSON.NET):
public class Foo
{
public int bar { get; set; }
public string baz { get; set; }
public string quxx { get; set; }
}
...
// json = {"bar": 1, "baz":"two", "quxx":"three"}
var result = await JsonConvert.DeserializeObjectAsync<Foo>(json);
return result.baz // "two"
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚我需要做些什么才能使它与复杂的结构一起工作.
var jObj = JsonConvert.DeserializeObject(json) as JObject;
var result = jObj["data"].Children()
.Cast<JProperty>()
.Select(x => new {
Title = (string)x.Value["title"] ,
Author = (string)x.Value["author"],
})
.ToList();
Run Code Online (Sandbox Code Playgroud)