Sunday, November 16, 2014

The easy way to work with JSON data in C#

A few years ago, the dominant way to exchange data between systems online was XML. XML can be a complicated and convoluted format to work with and, as with most complicated and convoluted format, it quickly fell out of favor as soon as something better came alone. That something better was a new format that was made for the web called JSON (short for JavaScript Object Notation).

JSON provides a simple, human readable, intuitive way to exchange almost any kind of data. Unlike XML, JSON data is small, easy to understand, and infinitely flexible. Best of all, working with JSON in most programming languages is amazingly simple. It's the perfect format for a modern world, populated with modern programmers.

Today, we're going to look at a topic I seem to get a lot of questions about: using JSON data in C#. Technically, JSON formatted data is called serialized and the process of turning into something you can easily work with is called deserialization. Today, we're going to look at how to deserialize JSON data. In the next article, I'll cover serializing data so that you can easily create your own web services.

First things first, C#'s native JSON parser sucks!

If you've done any research into working with JSON data in .NET, you've probably seen the built in .NET solution you know it's definitely not up to the task of everyday use. So we're going to use the open source JSON.NET library from James  King. It's an excellent library that's well supported and constantly updated. It's widely agreed that it's the best solution for working with JSON data. You can get it here.

How to deserialize JSON data in C# with JSON.NET

While there are a few ways to deserialize JSON data with JSON.NET, we're going to look specifically at what I consider the easiest way: modeling the data in a class then accessing class members as regular properties.

Let's say you have the following JSON string you want to process and you've stored it in a string variable called response:

    "Name" : "Tom Jones",
    "Age" : 26,
    "Spouse": "Jane Jones"

First, let's create a class that models the JSON data we want to process. It's amazingly easy:

public class JSONData{
    string Name {get; set;}
    int Age{get; set;}
    string Spouse{get; set;}

That's it. Just a getter and a setter for each JSON field we want to process. Nothing more needed. Now, let's access that data from our main class:

JSONData jData = Newtonsoft.Json.JsonConvert.DeserializeObject<JSONData> (response);

From there, we can access the different fields like this:


It's really that simple. Using JSON with the power of the JSON.NET library and C# makes a developers life amazingly simple! Now, there's nothing stopping you from going and grabbing data from any API on the planet.

Have fun!

1 comment:

Reggie A Pauly said...

The new article is important to show your customers your new trend and technologies. The peoples choose the essay writing service to share their latest news at this custom essay writing service . I think that this trend is so common that ordinary guys also like to do that.