105

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.

2

4 Answers 4

150

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

Sign up to request clarification or add additional context in comments.

4 Comments

I just added an example that should be useful. It is a mystery to me why the "type" is a list of lists (array array) - but this should align with the json string you gave.
Thanks a lot for your answer. It helped me to resolve my issue :)
I was using a cast which was failing MyData tmp = (MyData)JsonConvert.DeserializeObject(json);. The generic version of deserialize works much better, thanks. :-)
Ok, I write it here: dotnet add package Newtonsoft.Json and using Newtonsoft.Json;.
101

A much easier solution: Using a dynamic type

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.

4 Comments

nice answer, thanks for introducing me to dynamic : )
It's definitely easy. But it has the disadvantage of not being able to easily check if a property exists (you should use exception handling).
@Jowen To check if a property exists without exception handling have a look at those answers
thank you very much 1!! simplest solution ive been looking for so long
13

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

3 Comments

Now that's smart! ;-) But it gets clumsy when the definition isn't as straight-forward as your example.
I don't know anything about definition, how should I use DeserializeAnonymousType in this case?
This is a worthless example, as most JSON isn't anywhere near that simplistic. I'd like to see a definition that more closely resembles a JSON string with nested properties...
5
//Your snippet
object JsonDe = JsonConvert.DeserializeObject(Json);

//what you need to do
JObject JsonDe = JsonConvert.DeserializeObject<JObject>(Json);

Now you have and object with suitable properties and methods to work with the data.

You could also use Dictionary<string,object> instead of JObject. However, there are other alternatives, strongly-type though.

NewtonSoft.Json is an excellent library. I have used it for many use cases.

The beauty of json is that one can create schemas dynamically. Therefore we need to be able to write generic code to work with them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.