Skip to content

Instantly share code, notes, and snippets.

@drasticactions
Created March 16, 2017 17:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drasticactions/b68a68e9d84ad4abd26addc33b8429a7 to your computer and use it in GitHub Desktop.
Save drasticactions/b68a68e9d84ad4abd26addc33b8429a7 to your computer and use it in GitHub Desktop.
JSON.NET Deserialize Example
uti platforms packages
com.xamarin.workbook
Console
id version
Newtonsoft.Json
9.0.1

JSON.NET Deserialize Example

First, we need to get some JSON. Usually, this comes from a REST endpoint. For example, the result could look like this…

{ "foo": "bar", "butts": true, "numbers": [ 3, 4 ], "number": 3 }

Instead of calling an endpoint, however, I’m going to hardcode the result into a string.

var result = "{\"foo\":\"bar\",\"butts\":true,\"numbers\":[3,4],\"number\":3}";

Now that we have our result set, we now want to deserialize it into an object. There are several ways to do this, but most commenly, we use JSON.NET. With it, we can serialize and deserialize JSON. First, we can create types and deserialize our JSON to create full objects.

#r "Newtonsoft.Json"
#r "Microsoft.CSharp"
using Newtonsoft.Json;

public class RootObject
{
    [JsonProperty("foo")]
    public string Foo { get; set; }
    
    [JsonProperty("butts")]
    public bool Butts { get; set; }
    
    [JsonProperty("numbers")]
    public int[] Numbers { get; set; }
    
    [JsonProperty("number")]
    public int Number { get; set; }
}

This creates our typed object, which we can then feed into JSON.NET. The JsonProperty attribute is used so JSON.NET knows what field deserializes into what. You don’t have to give it however. If you name your object exactly like the outputted JSON, it will be deserialized as well. But this results in ugly types, and if we’re bothering with types at all the names might as well look good.

using Newtonsoft.Json;

RootObject typedObject = JsonConvert.DeserializeObject<RootObject>(result);

With this, we get full intellisense on the object, since it’s just a `RootObject`. This works with nested classes as well, so long as it’s apart of the class, it will get deserialized.

Of course, we can avoid types by using `dynamic`. `dynamic` won’t give us intellisense, but for very complex JSON it’s much easier to handle.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

dynamic dynamicResultObject = JsonConvert.DeserializeObject(result);

The results here are all `JObject` and `JProperty`, which is quite ugly. But if we know the type on the object already, we can just call it directly.

string foobar = dynamicResultObject.foo;

For some JSON types that are very hard to turn into C# classes, using `dynamic` is the best way to go.

@kiquenet
Copy link

Any sample using DateTime property ? issues about datetime format ?

@radebg
Copy link

radebg commented Jun 24, 2018

Wow, all my efforts to deserialize json file created by ffprobe solved by single line of code You provided!!

Thanks in a million!

Finally I can move on with my code

@Clatanii
Copy link

Clatanii commented Apr 15, 2020

Awesome, short and simple! Love it!

After finding nothing after a few hours I am so glad that I finally found this, Thanks!!!

@lusbyc
Copy link

lusbyc commented Nov 18, 2021

This is really helpful! Any suggestions for doing the same directly with a nested class? I have a json string with multiple objects, arrays, and nested objects. There's a particular nested object in my json that I want to deserialize without the other data. Is this possible or do I have to somehow parse the json down first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment