简体   繁体   English

如何解析C#中的JSON数组值(Windows Phone 7)?

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

I'm working in WP7. 我在WP7工作。 I need to parse JSON array value in to list box. 我需要将JSON数组值解析为列表框。 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? 有人说,使用Serializer和Deserializer,但我不知道如何使用serilizer和反序列化器将这些值解析为组合框或列表框?

I would suggest using JSON.NET - I've used that with no problems in Windows Phone 7. 我建议使用JSON.NET - 我在Windows Phone 7中没有遇到任何问题。

Don't focus on the list box to start with - focus on converting from JSON to your own type. 不要专注于列表框开始 - 专注于从JSON转换为您自己的类型。 Then separately deal with how to show a collection of objects of that type in your list box. 然后单独处理如何在列表框中显示该类型的对象集合。

 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
 }

Here's an example using the DataContractJsonSerializer . 这是使用DataContractJsonSerializer的示例。 However, for improved performance you should consider using Json.Net . 但是,为了提高性能,您应该考虑使用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. 然后,您可以迭代对象中的项目并将它们添加到列表框中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM