Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WP7中反序列化JSON_C#_.net_Json_Windows Phone 7_Json.net - Fatal编程技术网

C# 在WP7中反序列化JSON

C# 在WP7中反序列化JSON,c#,.net,json,windows-phone-7,json.net,C#,.net,Json,Windows Phone 7,Json.net,我有这个JSON,我正试图在Windows Phone上阅读它。我一直在玩DataContractJsonSerializer和Json.NET,但运气不太好,尤其是阅读每个“条目”: {"lastUpdated":"16:12","filterOut":[],"people": [{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"}], "serviceD

我有这个JSON,我正试图在Windows Phone上阅读它。我一直在玩
DataContractJsonSerializer
和Json.NET,但运气不太好,尤其是阅读每个“条目”:

{"lastUpdated":"16:12","filterOut":[],"people":
[{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"}],
 "serviceDisruptions":
  {
    "infoMessages":
    ["blah blah text"],
    "importantMessages":
    [],
    "criticalMessages":
    []
  }
}
我所关心的是人物部分的条目。基本上,我需要读取和迭代条目(包含ID、Name、Age值)并将它们添加到集合或类中。(之后我将填充一个列表框。)


任何指针都值得欣赏。

我能够使用以下代码对JSON字符串进行反序列化。这是在.NET4控制台应用程序中测试的,希望也能在WP7中使用

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PersonCollection));

string json =  "{\"lastUpdated\":\"16:12\",\"filterOut\":[],\"people\": [{\"ID\":\"a\",\"Name\":\"b\",\"Age\":\"c\"},{\"ID\":\"d\",\"Name\":\"e\",\"Age\":\"f\"},{\"ID\":\"x\",\"Name\":\"y\",\"Age\":\"z\"}], \"serviceDisruptions\": { \"infoMessages\": [\"blah blah text\"], \"importantMessages\": [], \"criticalMessages\": [] } }";

using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
    var people = (PersonCollection)serializer.ReadObject(stream);

    foreach(var person in people.People)
    {
        Console.WriteLine("ID: {0}, Name: {1}, Age: {2}", person.ID, person.Name, person.Age);
    }
}   
使用以下数据类:

[DataContract]
public class PersonCollection
{
    [DataMember(Name = "people")]
    public IEnumerable<Person> People { get; set; }
}

[DataContract]
public class Person
{
    [DataMember]
    public string ID { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Age { get; set; }
}
[DataContract]
公共类个人集合
{
[DataMember(Name=“people”)]
公共IEnumerable人{get;set;}
}
[数据合同]
公共阶层人士
{
[数据成员]
公共字符串ID{get;set;}
[数据成员]
公共字符串名称{get;set;}
[数据成员]
公共字符串年龄{get;set;}
}

下面的解决方案使用Json.NET。它首先将JSON字符串反序列化为XML,然后使用LINQ to XML迭代所有人员节点,并将它们转换为
Person
类的实例

private class Person
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
}

// deserializes your JSON and creates a list of Person objects from it
private void button1_Click(object sender, RoutedEventArgs e)
{
    // your JSON
    string json =
        "{\"lastUpdated\":\"16:12\",\"filterOut\":[],\"people\": " +
        "[{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"},{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"},{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"}]," +
        "\"serviceDisruptions\":" +
        "{" +
        "\"infoMessages\":" +
        "[\"blah blah text\"]," +
        "\"importantMessages\":" +
        "[]," +
        "\"criticalMessages\":" +
        "[]" +
        "}" +
        "}";

    // deserialize from JSON to XML
    XDocument doc = JsonConvert.DeserializeXNode(json, "root");

    // iterate all people nodes and create Person objects
    IEnumerable<Person> people = from person in doc.Element("root").Elements("people")
                                 select new Person()
                                 {
                                     ID = person.Element("ID").Value,
                                     Name = person.Element("Name").Value,
                                     Age = person.Element("Age").Value
                                 };

    // this is just demonstrating that it worked
    foreach (Person person in people)
        Debug.WriteLine(person.Name);
}
这就是反序列化JSON作为XML文档的样子(对于好奇的人来说):


16:12
x
x
x
x
x
x
x
x
x
废话

您的示例不是有效的json,请发布真实内容:-)好的,我刚刚更改了文本并将其格式化为单独的行。为什么你说它是无效的?这是完美的谢谢你,我很好地修改了它来填充我的收藏。对我来说,棘手的一点是如何列举它,但PersonCollection似乎很好地解决了这个问题。
using Newtonsoft.Json;
using System.Xml.Linq;
using System.Diagnostics;