Deserializing JSON in WP7

前端 未结 2 859
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 07:23

I have this JSON which I am trying to read on Windows Phone. I\'ve been playing with DataContractJsonSerializer and Json.NET but had not much luck, especially r

相关标签:
2条回答
  • 2021-01-13 08:09

    I was able to deserialize your JSON string using the following code. This was tested in a .NET 4 console application, and hopefully will work in WP 7 as well.

    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);
        }
    }   
    

    Using the following data classes:

    [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; }
    }
    
    0 讨论(0)
  • 2021-01-13 08:17

    The solution below uses Json.NET. It deserializes the JSON string first to XML, then uses LINQ to XML to iterate all people nodes and convert them to instances of the Person class.

    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);
    }
    

    Don't forget the imports:

    using Newtonsoft.Json;
    using System.Xml.Linq;
    using System.Diagnostics;
    

    And this is how the deserialized JSON looks like as XML document (for the curious people out there):

    <root>
      <lastUpdated>16:12</lastUpdated>
      <people>
        <ID>x</ID>
        <Name>x</Name>
        <Age>x</Age>
      </people>
      <people>
        <ID>x</ID>
        <Name>x</Name>
        <Age>x</Age>
      </people>
      <people>
        <ID>x</ID>
        <Name>x</Name>
        <Age>x</Age>
      </people>
      <serviceDisruptions>
        <infoMessages>blah blah text</infoMessages>
      </serviceDisruptions>
    </root>
    
    0 讨论(0)
提交回复
热议问题