我是Json的新手,並試圖瞭解如何使用Json.Net解析它。我試圖爲我的JSON輸入創建對象,但我被卡住了。我不太清楚如何解析輸入,所以我可以遍歷它並輸出季節數字和劇集名稱。使用Json.Net解析嵌套的Json
任何人都可以指出我正確的方向嗎?
JSON:
{
"data":{
"1921":{
"1":{
"airdate":"1921-03-20",
"name":"Cleaning Up!!?",
"quality":"N/A",
"status":"Wanted"
},
"2":{
"airdate":"1921-03-20",
"name":"Kansas City Girls Are Rolling Their Own Now",
"quality":"N/A",
"status":"Wanted"
},
"3":{
"airdate":"1921-03-20",
"name":"Did You Ever Take a Ride Over Kansas City Street 'in a Fliver'",
"quality":"N/A",
"status":"Wanted"
},
"4":{
"airdate":"1921-03-20",
"name":"Kansas City's Spring Clean-Up",
"quality":"N/A",
"status":"Wanted"
}
},
"1923":{
"1":{
"airdate":"2013-05-16",
"name":"Alice's Wonderland - aka - Alice in Slumberland",
"quality":"Unknown",
"status":"Downloaded"
}
}
},
"message":"",
"result":"success"
}
代碼:
static void Main(string[] args)
{
RootObject data = JsonConvert.DeserializeObject<RootObject>(System.IO.File.ReadAllText(@"C:\Users\Benjamin\Desktop\json\input.txt"));
foreach (var e in data)
{
// Being able to output Season and Episode name like:
// 1921 - Cleaning Up!!?
}
}
public class RootObject
{
public Dictionary<int, Season> data { get; set; }
public string message { get; set; }
public string result { get; set; }
}
public class Season
{
public Dictionary<string, Episode> number { get; set; }
}
public class Episode
{
public string airdate { get; set; }
public string name { get; set; }
public string quality { get; set; }
public string status { get; set; }
}
你究竟在遇到什麼麻煩?你有錯誤嗎? – mason 2014-10-06 19:41:20
反序列化後數據的外觀如何?它是否填充?在你的'RootObject'中,你有一個名爲'data'的Dictionary,所以你需要迭代'data.data'開始。 – 2014-10-06 19:42:28
是的,RootObject是否填充?你是否遇到異常?你的代碼看起來正確。 – nterry 2014-10-06 19:46:01