find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

How to Serialize and Deserialize JSON in C#

How to Serialize and Deserialize JSON in C#

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is the best for use easily read and written by humans and parsed and generated by machines. The application/json is the official Internet media type for JSON and filename extension is .json.

The Most of JSON files used for data transmission between various systems contain more complex data structures. Thereby, learning about the complex JSON structure will help you in creating test data based on the JSON schema requirements

How to work JSON in c#

I have some JSON format data which is return by sql server .

How to return JSON Format by SQL server: https://www.findandsolve.com/articles/json-in-sql-server-which-returns-json-nested-arrary

{
   "MenuList":[
      {
         "Name":"First Menu",
         "SubMenuList":[
            {
               "Name":"First SubMenu 1"
            },
            {
               "Name":"First SubMenu 2"
            }
         ]
      },
      {
         "Name":"Second Menu",
         "SubMenuList":[
            {
               "Name":"Second SubMenu 1"
            },
            {
               "Name":"Second SubMenu 2"
            }
         ]
      }
   ]
}

Now I am try to append JSON data in my view model or deserialize json c#. It is very easy to bind dynamic data in your view model. JsonConvert.DeserializeObject convert json data in view model without mapping each properties in c# json. There are different type of convert JSON to c#,I will see you some example to convert JSON to view model.

I have some JSON values as given above get JSON format by sql server. How to return JSON Fromat from SQL Server you can see JSON Format Return by sql server https://www.findandsolve.com/articles/json-in-sql-server-which-returns-json-nested-arrary

Now I am going to create ViewModel class in c# which class properties live as JSON Format.Here is my viewmodel class as given below

Create MenuViewModel and SubMenuViewModel Class. Then call SubMenuViewModel Class under the MenuViewModel class depend on JSON Format structure as given above.

       public class MenuViewModel
        {
            public List<SubMenu> MenuList { get; set; }
        }

In the MenuViewModel class I have called SubMenuViewModel and make a list as MenuList.

       public class SubMenuViewModel
        {
            public string Name { get; set; }
            public List<SubMenuViewModel> SubMenulist { get; set; }
        }

In the SubMenuViewModel I have created SubMenuList which is depend on JSON format as give above

Now I am ready to Mapping or convert JSON Format in My View model like as:

   var jsonFormateData = "{'MenuList':[{'Name':'First Menu','SubMenuList':[{'Name':'First SubMenu 1'},{'Name':'First SubMenu 2'}]},{'Name':'Second           Menu','SubMenuList':  [{'Name':'Second SubMenu 1'},{'Name':'Second SubMenu 2'}]}]}";
   var model = new MenuViewModel();
model = JsonConvert.DeserializeObject<MenuViewModel>(jsonFormateData);

Output


Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response