100

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.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
abc cba
  • 2,563
  • 11
  • 29
  • 50
  • possible duplicate of [Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class](http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class) – p.s.w.g Jun 11 '13 at 07:39
  • You should have a look at the answer to this question: http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class – Dietz Jun 11 '13 at 07:40

4 Answers4

146

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

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
Michael Banzon
  • 4,879
  • 1
  • 26
  • 28
  • 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. – Michael Banzon Jun 11 '13 at 12:32
  • 1
    Thanks a lot for your answer. It helped me to resolve my issue :) – santosh kumar patro Jul 13 '15 at 19:09
  • 1
    I was using a cast which was failing `MyData tmp = (MyData)JsonConvert.DeserializeObject(json);`. The generic version of deserialize works much better, thanks. :-) – SharpC Mar 19 '19 at 15:37
  • Ok, I write it here: `dotnet add package Newtonsoft.Json` and `using Newtonsoft.Json;`. – dani herrera Apr 07 '19 at 17:15
96

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.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 12
    nice answer, thanks for introducing me to `dynamic` : ) – maialithar Apr 23 '14 at 12:53
  • 2
    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 Jul 07 '14 at 13:12
  • 3
    @Jowen To check if a property exists without exception handling have a look at [those answers](http://stackoverflow.com/questions/2839598/how-to-detect-if-a-property-exists-on-a-dynamic-object-in-c) – Yves M. Jul 08 '14 at 10:04
  • thank you very much 1!! simplest solution ive been looking for so long – Sly_TheKing Sep 12 '16 at 13:30
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
SomethingOn
  • 9,813
  • 17
  • 68
  • 107
  • Now that's smart! ;-) But it gets clumsy when the definition isn't as straight-forward as your example. – Sнаđошƒаӽ Jun 18 '17 at 14:12
  • I don't know anything about `definition`, how should I use `DeserializeAnonymousType` in this case? – Mehdi Dehghani Sep 02 '17 at 05:05
  • 1
    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... – pmfith May 07 '18 at 19:23
3
//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

CFreitas
  • 1,647
  • 20
  • 29
yoandrygc
  • 31
  • 3