Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 ..
Posted
Updated 20-Sep-15 19:59pm
v4

This string is in JSON format. Look for a JSOn parser. I use newtonsoft. It's well documented, easy to use and best of all - FREE ^_^


http://www.newtonsoft.com/json[^]

Don't forget to check the license though:
https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md[^]
 
Share this answer
 
C#
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);
 
Share this answer
 
You can use the folowing
C#
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);
 
Share this answer
 
v4
Comments
Aditya_Goud 21-Sep-15 2:00am    
thankyou but , can you please tell me for windows mobile..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900