Newtonsoft JSON Deserialize

abc cba picture abc cba · Jun 11, 2013 · Viewed 239k times · Source

My JSON is as follows:

{"t":"1339886","a":true,"data":[],"Type":[['Ants','Biz','Tro']]}

I found the Newtonsoft JSON.NET deserialize library for C#. I tried to use it as follow:

object JsonDe = JsonConvert.DeserializeObject(Json); 

How can I access to the JsonDe object to get all the "Type" Data? I tried it with a loop but it is not working because the object does not have an enumerator.

Answer

Michael Banzon picture Michael Banzon · Jun 11, 2013

You can implement a class that holds the fields you have in your JSON

class MyData
{
    public string t;
    public bool a;
    public object[] data;
    public string[][] type;
}

and then use the generic version of DeserializeObject:

MyData tmp = JsonConvert.DeserializeObject<MyData>(json);
foreach (string typeStr in tmp.type[0])
{
    // Do something with typeStr
}

Documentation: Serializing and Deserializing JSON