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.
This question is related to
c#
json.net
deserialization
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
As of Json.NET 4.0 Release 1, there is native dynamic support.
You don't need to declare a class, just use dynamic
:
dynamic jsonDe = JsonConvert.DeserializeObject(json);
All the fields will be available:
foreach (string typeStr in jsonDe.Type[0])
{
// Do something with typeStr
}
string t = jsonDe.t;
bool a = jsonDe.a;
object[] data = jsonDe.data;
string[][] type = jsonDe.Type;
With dynamic you don't need to create a specific class to hold your data.
As per the Newtonsoft Documentation you can also deserialize to an anonymous object like this:
var definition = new { Name = "" };
string json1 = @"{'Name':'James'}";
var customer1 = JsonConvert.DeserializeAnonymousType(json1, definition);
Console.WriteLine(customer1.Name);
// James
Source: Stackoverflow.com