對不起,如果這是之前問過,但我試圖尋找一些答案,但我找不到一個。在C#中使用JSON.NET或JavaScriptSerializer反序列化JSON對象
反正我也有類似的問題,這個問題: Parsing JSON using Json.net
它解決了一些問題,但感到困惑我的JSON數據。這裏是json數據
{ "messages":[ { "message":"hilo", "timestamp":"23:55", "uid1":"7", "name":"shiftypowers" } ], "total_messages":"1", "last_timestamp":"1304265309", "buddylist":{ "476":{ "name":"Gandang_hari", "status":"1" }, "506":{ "name":"ichigo", "status":"1" }, "186":{ "name":"Jinn", "status":"1" }, "7":{ "name":"shiftypowers", "status":"1" }, "total":4 } }
如果您注意到數字476,506,186和7--這些是用戶ID,並且將在未來處理數千個用戶ID。
問題是,我該如何反序列化?
這裏是我的代碼
public class ElakoChatPollData { public ElakoChatPollData() { messages = new List(); } public List messages { get; set; } public string total_messages { get; set; } public string last_timestamp { get; set; } public buddylist buddylist { get; set; } } public class messages { public string message { get; set; } public string timestamp { get; set; } public string uid1 { get; set; } public string name { get; set; } } public class buddylist { // what should I put here ?? //public List uid { get; set; } // i don't think this is correct public Dictionary Users { get; private set; } public string total { get; set; } } public class userinfo { public string name { get; set; } public string status { get; set; } } // and serializing the json looks like this JavaScriptSerializer ser = new JavaScriptSerializer(); ChatPollData epd = ser.Deserialize<ChatPollData>(jsonchatpoll); Console.Writeline(epd.buddylist.Users[0].name); // i get Object reference not set ~~~~
如果上面的代碼是正確的。如何可以我能夠得到用戶的id?和順便說一句,json的數據將從drupal模塊(drupalchat)請求
你能修改上面的JSON來包含這樣的uid嗎? '「uid」:「476」, 「userinfo」:{ 「name」:「Gandang_hari」, 「status」:「1」 }' – 2011-05-01 17:54:25
nope。上述JSON數據將通過網址從drupalchat模塊請求 – jaysonragasa 2011-05-01 17:56:00
請我需要一個答案 – jaysonragasa 2011-05-02 06:29:02