How to deserialize JSON object to different types using Newtonsoft.Json?

I’ve recently publish few posts that explains how to work with Newtonsoft.Json framework. Make sure you check them out if you would like to learn more. In this post, I’ll tell you how to deserialize JSON object to different types using NewtonSoft.Json.

Deserialization is really simple using this framework when you are dealing with types that are known. There can be times when the type is not known and still the JSON response needs to be read and processed in some manner.

One of the example is getting a response from an API where the keys are dynamic or the structure is quite different that you don’t have a concrete type for it to process. But don’t worry as Newtonsoft.Json has got you covered for such cases.

I’m using a console application and my Program.cs file looks like this:

As you can see above, I’ve a class named DifferentTypesDemo that has a static method named ShowDemo, its implementation is shown below:

Let us understand at the code above.

  • Line no. 5 defines a string object that has the JSON representation for data.
  • Line no. 13 is the common way of using JsonConvert and deserializing the data in string object to a .NET object. This can be used along with a specific type in cases where a type is known.
  • Newtonsoft.Json framework also supports deserializing the data into a Dictionary. In this case, it is of type Dictionary<string, string>. Important thing to remember over here is that not everything can be deserialized into a dictionary.

    This is really useful in cases when an API returns a response that is a collection and the key of the each item is dynamic – may be an int or string. So, this way of deserialization can be used to convert that data into a dictionary. And then loop over it to process it.

  • One other way to deserialize using this framework is to use DeserializeAnonymousType method of JsonConvert. C# allows us to create anonymous types on the fly without creating a class. Line no. 19 defines such an anonymous object.

    Next, on line no. 26, the author data is then deserialized using this special method. The resulting object will only have those members that were defined in anonymous type definition.

    The last option is useful when you only want some part of the data to be converted to a .NET object. The non matched members within data will be ignored.

This framework is really great to deal with JSON data and .NET objects. I hope this post explains how to deserialize JSON object to different types using Newtonsoft.Json properly.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger