Igor Kulman

Processing JSON in .NET

· Igor Kulman

JSON is a very popular format for exchanging data, especially in the world of web technologies and JavaScript. The .NET platform contains a native support for this format but a better alternative is to use the JSON.NET library.

JSON.NET

JSON.NET is a flexible JSON serializer for .NET with LIQN support. The biggest reason for using this library is its performance in comparison to the standard DataContractJsonSerializer. The easiest way to use the library is to use the Nuget package.

JSON.NET is very easy to use; first you create a JObject instance

JObject o = JObject.Parse(jsonString);

from which you can get a collection (in our example of type TicketViewModel)

Tickets = new ObservableCollection<TicketViewModel>((o["tickets"].Select(ticket => new TicketViewModel(ticket)).OrderBy(l => l.City).ToList()));

filling the TicketViewModel instance from the JObject

public TicketViewModel(Newtonsoft.Json.Linq.JToken ticket) 
{
    _city = (string)ticket.SelectToken("city"); 
    _cityCes = (string)ticket.SelectToken("city_ces");      
    _currency = (string)ticket.SelectToken("currency");  
    .... 
} 

DataContractJsonSerializer

If you do not want or cannot use JSON.NET you can use the already mentioned DataContractJsonSerializer. Before you use it, you have to create a class with the same structure as the JSON. You do not have to create it by hand; you can use the json2csharp utility.

Using the DataContractJsonSerializer is a less readable than using JSON.NET

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Tickets)); 
Tickets tickets; 
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(responseText))) 
{
    root = serializer.ReadObject(stream) as Tickets; 
}

See also