How to convert JSON objects into c# classes

Today I will show you that how can we automatically convert JSON formatted objects into c# classes so that we can use them in c# our codes.
There is one tool which will convert JSON object into c# classes for you automatically.Actually it is website http://json2csharp.com/ which is developed by Jonathan Keith with the help of JSON C# Class Generator project and James Newton-King’s Json.NET

json to c#

Tool is very Simple to use. You just have to paste your JSON object & click on Generate Button.
If your JSON has any errors then It will show error message something like this.

json to c# error

If your JSON is correctly formatted then It will convert it into c# classes.
Lets take simple example.
I have simple valid JSON formatted message which I want to convert into c# class so that I can use it in my code.

{
 "menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
 }
}

As it is valid JSON , tool will convert it into c# classes which will look something like this

public class Menuitem
{
    public string value { get; set; }
    public string onclick { get; set; }
}

public class Popup
{
    public List<Menuitem> menuitem { get; set; }
}

public class Menu
{
    public string id { get; set; }
    public string value { get; set; }
    public Popup popup { get; set; }
}

public class RootObject
{
    public Menu menu { get; set; }
}

So like this you can convert JSON object into c# classes very easily using this tool.

admin