我有一個看起來像這樣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
}
從這個數據我想顯示像這樣的列表:
Alpha, Justin X. Ample
Tau, Felina Blank
注意,鑰匙(在我的例子中,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"
但我想不出什麼,我需要做的,使之具有複雜結構的工作。
如果您使用Visual Studio 2012,得到[ASP.NET和Web Tools 2012.2(http://go.microsoft.com/fwlink/?LinkID=275131)。它有一個名爲「將JSON粘貼爲類」的功能,它將從剪貼板中的JSON生成一個C#類。 – Oded