a blog for those who code

Friday 22 July 2016

Getting started with JSON using C#

In this post we will be discussing about getting started with JSON using C#. JavaScript Object Notation (a.k.a JSON) is a lightweight data-interchange format designed for human-readable data interchange. The advantage of using JSON is that it is language independent, the syntax is same as that of JavaScript objects and it can be used to store application data.

JSON is used for serializing and transmitting structured data over network connections and it is very popular in Web services and APIs. This is used because it is very light weight, easy to read and is language independent.

JSON Syntax


JSON values consists of Strings, Numbers, Boolean (True or False), Null, Objects and even Arrays.

{
  "name": "Coding Defined",
  "location": "India",
  "created": "18 June 2014",
  "tutorials": [
    {
      "name": "Nodejs",
      "numbers": 150,
      "isAvailabe":  true
    }
  ]
}

ALSO READ : Generating C# Classes from XML Or JSON

In the above example you can see we have strings ("name": "Coding Defined"), number ("numbers": 150), bollean ("isAvailabe":  true).

JSON Objects is a collection which have name/value pairs using colons. They are enclosed by curly braces and they are separated by commas as shown below :

{
  "name": "Nodejs",
  "numbers": 150,
  "isAvailabe":  true
}

JSON Arrays are values enclosed by brackets and they are separated by commas. Array can be a collection of anything strings, numbers or even objects. Some examples or array are shown below

[2014,2015,2016]

["A","B","C"]

[
 {
   "name1":"value1",
   "name2":"value2"
 },
 {
   "name1":"value3",
   "name2":"value4"
 },
 {
   "name1":"value5",
   "name2":"value6"
 },
]

In simple terms JSON objects and arrays can be nested.

How to parse JSON files in C#


We will be parsing our JSON files in C# using JavsScriptSerializer Class. At first you need to add a reference of System.Web.Extensions in your application and add 'using System.Web.Script.Serialization;' namespace as shown below :


New we are going to Deserialize a json file.

Demo.Json

{
  "name": "Coding Defined",
  "location": "India",
  "created": "18 June 2014",
  "years": [2014,2015,2016],
  "tutorials": [
    {
      "name": "Nodejs",
      "numbers": 150,
      "isAvailabe":  true
    }
  ]
}

To Deserialize it we have to create a Class in C# as shown below :

class JSONDemo
{
  public string name { get; set; }
  public string location { get; set; }
  public string created { get; set; }
  public Int32[] years { get; set; }
  public List<Tutorials> tutorials { get; set; }
}

class Tutorials
{
  public string name { get; set; }
  public int numbers { get; set; }
  public bool isAvailabe { get; set; }
}

Then we have to write below code to deserialize our JSON file

JavaScriptSerializer serializer = new JavaScriptSerializer();
JSONDemo demo = serializer.Deserialize<JSONDemo>(json);
Console.WriteLine(demo.name + " has created on " + demo.created + " at " + demo.location);

It will give you a result as shown below :


Now we will serialize our object to a json as shown below

List<Tutorials> tutorials = new List<Tutorials>();
Tutorials tut = new Tutorials {
 name = "JSON",
                  numbers = 1,
                  isAvailabe = true
                };
tutorials.Add(tut);

Int32[] array = new Int32[] {1,2,3};

JSONDemo jd = new JSONDemo
{
 name = "Hello",
 location = "UK",
 created = "20 July 2016",
 years = array,
 tutorials = tutorials
};

var jSONValue = serializer.Serialize(jd);
Console.WriteLine(jSONValue);

It will give you a result as shown below



Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment