JSON destabilization in WP7

I have JSON that I am trying to read on a Windows Phone. I played with DataContractJsonSerializerand Json.NET, but didnโ€™t have much luck, especially after reading each โ€œrecordโ€:

{"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":
    []
  }
}

All I care about is the entries in the People section. Basically, I need to read and iterate over records (containing the values โ€‹โ€‹of ID, Name, Age) and add them to the collection or class. (Then I fill out the list.)

Any pointers appreciated.

+5
source share
2 answers

I managed to deserialize the JSON string using the following code. This has been tested in the .NET 4 console application, and hopefully it 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);
    }
}   

:

[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; }
}
+6

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

:

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

JSON XML- ( ):

<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>
+1

All Articles