locked
Deserialize JSON data RRS feed

  • Question

  • Hi,

    I'm trying to deserialize JSON data into simple objects in WP7 app. I've used  DataContractJsonSerializer for that matter. It works for simple JSON data like: {"code":200,"msg":"true","data": "done" }.

    However, when I try to deserialize complex data like:

    {"code":200,"msg":"true","data":
    {"item":[{"title":"title1","url":"url1","points":0},{"title":"title2","url":"url2","points":20}]}}

    it fails. Any solution for this? I tried different ways & some solutions from this forum. But getting more confused.

    Thanks in advance.

    Tuesday, January 18, 2011 5:25 AM

All replies

  • Why not try the System.JSon deserializer instead? It handles any type of JSON stream and gives you the flexibility of parsing the results and building the corresponding object graph.

    Tuesday, January 18, 2011 6:40 AM
  • I couldn't find System.JSON. Am I missing any assembly reference? It's a WP7 application.

    Tuesday, January 18, 2011 11:28 AM
  • Yes, it's not registered in the registry so it doesn't show in the .NET tab but it works fine with Windows Phone 7.

    Just add the reference from:

    C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Libraries\Client to System.Json.dll. You might get a warning but that's fine, it is fine on the phone (remove the x86 if you are on a 32-bit machine).

    Tuesday, January 18, 2011 12:17 PM
  • Have you considered using JSON.Net? If you're passing considerable sizes of JSON (approx 5kb+) there's a noticable difference between DataContracJsonSerializer and the JSON.Net serializer by a factor of up to 3. There are WP7 apps reporting an entire 1 second speed up in data load times simply due to changing JSON serializers.   

    Tuesday, January 18, 2011 2:59 PM
  • +1 to Justin's recommendation, JSON.NET is much faster than the Microsoft ones and it rocks. Linq to Json is nice, too.

    Tuesday, January 18, 2011 3:19 PM
  • Thanks Jeremy.

    Now, using Json.NET library, I can easily deserialize the simple JSON into object. Like:

    {"code":200,"msg":"true","data": "done" }.

    Can be deserialized to following object:

    public class CustomObject
        {
            public int code { get; set; }
            public string msg { get; set; }
            public string data { get; set; }
        }

    But I'm unable to deserialize following text.

    {"item":[{"title":"title1","url":"url1","points":0},{"title":"title2","url":"url2","points":20}]}}

    Thanks for the help.

     


    However,  as I mentioned, for complex data types I couldn't do anything. May be I'm not very experienced with it.

     

    Wednesday, January 19, 2011 5:04 AM
  • Did you ever find a resolution to your problem?

    I'm running into the same situation with complex data types and I'm at my wits end.

    Saturday, February 19, 2011 3:20 PM
  • I just solved my similar problem. did you try something like the following:

    public partial class MyItems
    {
     public List<MyItem> item {get;set;}
    }
    
    public partial class MyItem
    {
     public string title {get;set;}
     public string url {get;set;}
     public int points {get;set;}
    }


    then you can deserialized it using the following:

    var myItems = JsonConvert.DeserializeObject<MyItems>(jsonString);


    Sunday, February 20, 2011 1:44 PM