Deserialize Json data using Newtonsoft Json.NET library

As we learn to make more and more advanced apps, we need more tools in out kitty to make an amazing app. Local apps work great but they can only do so much. Exchanging data from a server or web service is very essential to make a dynamic and constantly updated app. Let’s see how to work with Json in Windows Phone 8 app using the Json.NET library.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.

What is Json.NET library?

Json.NET is most popular, free and open source library to work with Json in Windows Phone or Windows apps. In this example, we will be learning how to parse Json data and store it in a variable which can be later used to bind that data to the UI elements such as a ListBox or LongListSelector
Let’s get started. I’ll be making a sample project with 1 style of Json and its parsing. But for the article I’ll be writing about one style, rest of the styles can be seen in the source code shared at the end of the article. Also, I won’t be covering DataBinding in this post, I shall cover that in a separate post. For the time being, the Json data is stored in a local text file stored in assets folder.

1. Adding Json.NET to your app

Go to the Nuget Package manager and search for Json.NET and add the Json.NET library to your project.

Nuget Package Manager
Adding Newtonsoft Json.NET

2. Add the following namespaces to your project

3. Convert the Json data into classes

To convert any Json data into it’s relevant classes, simple go to Json2Csharp and paste your data and this website will give you all the necessary classes. Or alternatively, yon can use the Visual Studio’s in built feature of Paste Json as Class feature from Edit -> Paste Special menu. This is my sample Json data used for this project:

Let’s understand what this data is, I’d suggest doing a search to understand the basic format of JSON data if you don’t already know. This is a object, mydata which is an array of objects. The objects in the array are all alike have 5 tags with different values in each object.

This is what the classes look like after converting the data into classes using Json2Csharp, don’t forget to paste these classes in you .cs file:

Now let’s take a moment to understand what these classes really mean. First class, MyData maps the object in the mydata array in our Json file. You can see the that this class has 5 variables with the same name as the our Json Objects in the mydata array. Second class, RootObject contains a list of class MyData, that means it acts like the array mydata as seen in our sample Json file. This RootObject is the container in which we will deserialize then Json data. It’ll then be a List of Json objects which we can use anyway we like. [DataBinding being a great use, which I will cover in another post.]

4. Read the Json data in a string

Since our file is locally hosted, we are simply reading the contents of the text file in a string using this function:
This function is called like this:

5. Deserialize

The final step.

Let’s see how and what really happened in the second statement(line 5). We created an object obj of class RootObject and then called DeserializeObject function for JsonConvert class and passed the variable containing our Json data as the parameter. That’s it and this data is now ready to be used in anyway you like. I’ll be making a tutorial on using this data with a DataBound LongListSelector soon.

Download source code from Github

Visit my website at