json ="{
"data": [
{
"id": "1000",
"from": {
"name": "Anthony Waema",
"category": "message",
"id": "192"
},
"message": "this is the message",
"updated_time": "2001-05-06T19:34:15+0000",
"likes": {
"data": [
{
"id": "100001692250255",
"name": "\u00dcnal Turanl\u0131"
},
{
"id": "100001060078996",
"name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
}]
},
{
"id": "10150186255753553",
"from": {
"name": "another name",
"category": "message",
"id": "100001"
},
"message": "this is the message",
"updated_time": "2001-04-06T19:34:15+0000",
"likes": {
"data": [
{
"id": "1002345",
"name": "\u00dcnal Turanl\u0131"
},
{
"id": "100234",
"name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
}]
}
}
]
}";
public class Allstatus
{
public List<sdata> data { get; set; }
public scpaging paging { get; set; }
}
public class sdata
{
public string id { get; set; }
public sfrom from { get; set; }
public string message { get; set; }
public string updated_time {get; set;}
public List<likesdata> likes { get; set; }
}
public class likesdata
{
public string id{ get; set; }
public string name{ get; set; }
}
public class sfrom
{
public string name {get; set;}
public string category {get; set;}
public string id {get; set;}
}
JavaScriptSerializer ser = new JavaScriptSerializer();
page = ser.Deserialize<allstatus>(json);
foreach (sdata cd in page.data)
{
foreach(likesdata ld in cd.likes.data)
{
Console.WriteLine(ld.id+"\t"+ld.name);
}
}
問題: 我需要解析json並檢索喜歡的數據。 我可以訪問「from」數據但不能「喜歡」數據..當我這樣做時,我得到nullreference錯誤。幫助需要在這裏..謝謝。使用system.web.script.seriarilization解析Json或使用json.net
除了使用.NET 2.0,是否真的有一個很好的理由使用外部庫而不是內置JavaScriptSerializer?除非絕對必要,否則我不會在應用程序中使用外部依賴關係。 – 2011-05-15 17:33:28
順便說一句,很好的答案。 +1 – 2011-05-15 17:33:48
@Kevin,JavaScriptSerializer位於System.Web內部,因此您將無法使用.NET 3.5及更高版本的客戶端配置文件。 – 2011-05-15 17:44:22