How to parse the JSON Array value in C# (Windows phone 7)?

余生长醉 提交于 2019-12-05 02:01:42

问题


I'm working in WP7. I need to parse JSON array value in to list box. Somebody said, use Serializer and Deserializer but i dont know how to parse those values in to combo box or list box using serilizer and deserializer?


回答1:


I would suggest using JSON.NET - I've used that with no problems in Windows Phone 7.

Don't focus on the list box to start with - focus on converting from JSON to your own type. Then separately deal with how to show a collection of objects of that type in your list box.




回答2:


 string MyJsonString ="{your JSON here}"; //JSON Result
 var ds = new DataContractJsonSerializer(typeof(City[]));
 var msnew = new MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
 City[] items = (City[])ds.ReadObject(msnew);
 foreach (var ev in items)
 {
   ComboCityBox.Items.Add((ev.name.ToString()));// binding name in to combobox
 }



回答3:


Here's an example using the DataContractJsonSerializer. However, for improved performance you should consider using Json.Net.

string jsonString = "{your JSON here}";

var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));

var serializer = new DataContractJsonSerializer(typeof(YourListObject));

var deserialized = (YourListObject)serializer.ReadObject(ms);

You could then iterate over the items in your object and add them to the listbox.



来源:https://stackoverflow.com/questions/4427473/how-to-parse-the-json-array-value-in-c-sharp-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!