Json.NET Examples


Json.NET is a popular high-performance JSON framework for .NET.

Benefits and Features:

  •     Flexible JSON serializer for converting between .NET objects and JSON
  •     LINQ to JSON for manually reading and writing JSON
  •     High performance, faster than .NET’s built-in JSON serializers
  •     Write indented, easy to read JSON
  •     Convert JSON to and from XML
  •     Supports .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store etc.

Let’s see some of the examples. Every example is self explanatory.

I have created a “User” class for demo purpose.


public class User
{
public int Age { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Address { get; set; }
}

Converting List to JSON:


List<User> userList = new List<User>();
User user = new User();
user.Age = 20;
user.Name = "Johny";
user.City = "California";
user.Address = "Ford street, New york.";

userList.Add(user);

User user1 = new User();
user1.Age = 20;
user1.Name = "Adam";
user1.City = "Sydney";
user1.Address = "xyz street, CA.";

userList.Add(user1);

string jsonString = JsonConvert.SerializeObject(userList);
Console.WriteLine(jsonString);

/* Output

[{"Age":20,"Name":"Johny","City":"California","Address":"Ford st
reet, New york."},{"Age":20,"Name":"Adam","City":"Sydney","Address":"xyz street,
CA."}]

*/

Converting an object to JSON:


User user2 = new User();
user2.Age = 20;
user2.Name = "Sachin";
user2.City = "Mumbai";
user2.Address = "New road, Mumbai.";

string jsonString = JsonConvert.SerializeObject(user2);
Console.WriteLine(jsonString);

/* Output

Serialize Object: {"Age":20,"Name":"Sachin","City":"Mumbai","Address":"New road, M
umbai."}

*/

Converting JSON to XML:


string json = @"{
'@Id': 1,
'Email': 'sachin@google.com',
'Active': true,
'CreatedDate': '2013-11-12T00:00:00Z',
'Roles': [
'User',
'Admin'
],
'Team': {
'@Id': 2,
'Name': 'Software Developers',
'Description': 'Creators of fine software products and services.'
}
}";

XNode node = JsonConvert.DeserializeXNode(json, "Root");
Console.WriteLine(node.ToString());

/* Output

<Root Id="1">
<Email>sachin@google.com</Email>
<Active>true</Active>
<CreatedDate>2013-11-12T00:00:00Z</CreatedDate>
<Roles>User</Roles>
<Roles>Admin</Roles>
<Team Id="2">
<Name>Software Developers</Name>
<Description>Creators of fine software products and services.</Description>
</Team>
</Root>

*/

Deserialize JSON to an Object:


string jsonString = @"{'Age':20,'Name':'John','City':'Mumbai','Address':'Man road, fashion street, Mumbai 400001.'}";

User objUser = JsonConvert.DeserializeObject<User>(jsonString);
Console.WriteLine(objUser.Address);

/* Output

Man road, fashion street, Mumbai 400001.

*/

Hope this helps. Please comment below if you want any specific example of JSON.NET library.

5 thoughts on “Json.NET Examples

  1. Hello,

    I am writing a BTCe trading bot in vb.net, I have the following JSON output that I want to parse and do stuff with.

    OpenOrders:

    {‘success’:1,’return’:{‘228658068’:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:10.00000000,’rate’:9.50000000,’timestamp_created’:1399343744,’status’:0},’228852139′:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:30.00000000,’rate’:10.00000000,’timestamp_created’:1399357276,’status’:0},’228854007′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:50.00000000,’rate’:11.00000000,’timestamp_created’:1399357409,’status’:0},’228855057′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:249.00000000,’rate’:11.50000000,’timestamp_created’:1399357486,’status’:0},’230145179′:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:50.00000000,’rate’:10.25000000,’timestamp_created’:1399482284,’status’:0},’230218473′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:30.00000000,’rate’:10.75000000,’timestamp_created’:1399490027,’status’:0},’230819253′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:18.00000000,’rate’:10.60000000,’timestamp_created’:1399554092,’status’:0}}}

    OrderList:

    {‘success’:1,’return’:{‘228658068’:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:10.00000000,’rate’:9.50000000,’timestamp_created’:1399343744,’status’:0},’228852139′:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:30.00000000,’rate’:10.00000000,’timestamp_created’:1399357276,’status’:0},’228854007′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:50.00000000,’rate’:11.00000000,’timestamp_created’:1399357409,’status’:0},’228855057′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:249.00000000,’rate’:11.50000000,’timestamp_created’:1399357486,’status’:0},’230145179′:{‘pair’:’ltc_usd’,’type’:’buy’,’amount’:50.00000000,’rate’:10.25000000,’timestamp_created’:1399482284,’status’:0},’230218473′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:30.00000000,’rate’:10.75000000,’timestamp_created’:1399490027,’status’:0},’230819253′:{‘pair’:’ltc_usd’,’type’:’sell’,’amount’:18.00000000,’rate’:10.60000000,’timestamp_created’:1399554092,’status’:0}}}

    getinfo:

    {‘success’:1,’return’:{‘funds’:{‘usd’:6.72764373,’btc’:0.02137639,’ltc’:1.64467243,’nmc’:9.798,’rur’:0,’eur’:0.20888816,’nvc’:0,’trc’:0,’ppc’:0,’ftc’:0,’xpm’:0.24732965,’cnh’:0,’gbp’:0},’rights’:{‘info’:1,’trade’:1,’withdraw’:0},’transaction_count’:22573,’open_orders’:7,’server_time’:1399583742}}

    Can you point me in the right direction to be able to view this information in an object and be able to loop through it.

    thanks

    Andrew

    Like

  2. My OS is Win 7, and I use Visual studio 2010 VB.Net.

    I’m using JSON to read xmptags with Exiftool. I have had to use the following code to retrieve tags that have a hyphen in the name. VB.Net will not allow hyphens in property names.

    NB:- json is the string returned by Exiftool.
    Dim deserializedProduct As Object = JsonConvert.DeserializeObject(Of Object)(json)
    ‘JsonConvert.serializeObject
    obj = JsonConvert.DeserializeObject(json)

    If obj.Item(“Writer-Editor”) Is Nothing Then
    frmXMPMetadata.txtWriterEditor.Text = “”
    Else
    frmXMPMetadata.txtWriterEditor.Text = obj.Item(“Writer-Editor”).ToString
    End If
    Pleas can you tell me where to find an example of writing back to a file, and if possible how to write an array as well.

    Thanks John

    Like

  3. Thank you for sharing us how to serialize a list of objects to JSON, but how can we deserialize that list so that it will not be an object but rather a list of objects.

    Like

Leave a comment