本文介绍了如何将json字符串解析为字典,Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器现在有以下响应,我需要解析它并将其放入字典中作为键/值

Ex:

键:1

价值:Andhrapradesh



[{stateid:1,statename:AndhraPradesh}, {stateid:2,statename:Arunachal Pradesh},{stateid:3,statename:Assam},{stateid:4,statename: 比哈尔邦},{ STATEID: 5, Statename的: 昌迪加尔},{ STATEID: 6, Statename的: 恰蒂斯加尔},{ STATEID: 7, statename:Dadra and Nagar Haveli},{stateid:8,statename:Daman and Diu},{stateid:9,statename:Delhi}, { STATEID: 10, Statename的: 果},{ STATEID: 11, Statename的: 古吉拉特},{ STATEID: 12, Statename的: Haryana}]

帮助我如何做到这一点..

I have the following response from my server now i need to parse this and place into a dictionary as Key/Value
Ex:
Key: 1
Value: Andhrapradesh

[{"stateid":"1","statename":"AndhraPradesh"},{"stateid":"2","statename":"Arunachal Pradesh"},{"stateid":"3","statename":"Assam"},{"stateid":"4","statename":"Bihar"},{"stateid":"5","statename":"Chandigarh"},{"stateid":"6","statename":"Chhattisgarh"},{"stateid":"7","statename":"Dadra and Nagar Haveli"},{"stateid":"8","statename":"Daman and Diu"},{"stateid":"9","statename":"Delhi"},{"stateid":"10","statename":"Goa"},{"stateid":"11","statename":"Gujarat"},{"stateid":"12","statename":"Haryana"}]
help me how can i do this ..

推荐答案


var str =@"[{""stateid"":""1"",""statename"":""AndhraPradesh""},{""stateid"":""2"",""statename"":""Arunachal Pradesh""},{""stateid"":""3"",""statename"":""Assam""},{""stateid"":""4"",""statename"":""Bihar""},{""stateid"":""5"",""statename"":""Chandigarh""},{""stateid"":""6"",""statename"":""Chhattisgarh""},{""stateid"":""7"",""statename"":""Dadra and Nagar Haveli""},{""stateid"":""8"",""statename"":""Daman and Diu""},{""stateid"":""9"",""statename"":""Delhi""},{""stateid"":""10"",""statename"":""Goa""},{""stateid"":""11"",""statename"":""Gujarat""},{""stateid"":""12"",""statename"":""Haryana""}]";
System.Web.Script.Serialization.JavaScriptSerializer JSS = new System.Web.Script.Serialization.JavaScriptSerializer();
var myDictionary = JSS.Deserialize<tempclass[]>(str).ToDictionary(k=> k.stateid, v=>v.statename);


JsonSerializer jsonSerializer = new JsonSerializer();
Hashtable ht = JsonConvert.DeserializeObject<Hashtable>(thejson);
Dictionary<string,> theDict = ht.Cast<DictionaryEntry> ()
                               .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);


这篇关于如何将json字符串解析为字典,Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:54