User Id :    Password :      New Member   Forgot Password
 
Understanding JSON serialization and deserialization in ASP.NET
Description While working on an ajax application we need to transfer data from server to client anynchnously. This data transfer from server to client can be done in variety of formats. The common data formats are XML and JSON.   No. of Views     5789
  Rating     0
Author Najmul Hoda   Posted On     23 Sep 2011
Tags ASP.NET,Java Script,JSON    

Sample Code   Download Code

 

Introduction

While working on an ajax application we need to transfer data from server to client anynchnously.
This data transfer from server to client can be done in variety of formats. The common data formats are XML and JSON.

When working with JSON format, the data from server are sent in JSON format and is cunsumed and parsed at client side by JAVAScript.

Before going into detail we must know basics of JSON. For that I would like readers to go through read my previous article Understanding JSON. This is will help in understanding this article more.

 

Serializing complex data structure in JSON format at server is not an easy task as we often make synactical mistakes. And finding error in JSON data is very tedious job.

But thanks to Microsoft for introducing JavaScriptSerializer class in .NET 3.5 to avoid the handy work of converting complex .Net data structure to JSON data structure and hence lead to no or very less error.

JavaScriptSerializer class is added in System.Web.Script.Serialization namespace so we must add this namespace in our code.

Lets take an example and discuss how we can deserialize(convert) an Employee class Object into JSON format and back the JSON data into Employee calss object.

Below is Employee class that we would convert into JOSN data and then back into Employee class object.

 

class Employee
   {
       public string EmpName { get; set; }
       public string Country { get; set; }
       public string[] Skills { get; set; }
       public string Project { get; set; }
   }



Now create a method GetEmplyees() that will assign values to the employee objects and return a list employees
 

public List GetEmplyees()
{
List objEmployeeList = new List();

Employee objEmployee;
objEmployee = new Employee();
objEmployee.EmpName = "Najmul";
objEmployee.Country = "India";
objEmployee.Skills = new string[] { "JAVA", "ORACLE" };
objEmployee.Project = "AsianPost";
objEmployeeList.Add(objEmployee);

objEmployee = new Employee();
objEmployee.EmpName = "Hoda";
objEmployee.Country = "US";
objEmployee.Skills = new string[] { "SQL", "ASP", "JSON", "WCF" };
objEmployee.Project = "LTR";
objEmployeeList.Add(objEmployee);

return objEmployeeList;
}



How to serialize employee object into JSON


Now create a method SerializeJSON() that will convert list of employee object into JSON object.
 

 private void SerializeJSON()
    {
        List objEmployeeListToSerialize = new List();
string strJSON = string.Empty;

// Call function GetEmplyees() to get employee list
objEmployeeListToSerialize = GetEmplyees();

JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();
strJSON = objJSSerializer.Serialize(objEmployeeListToSerialize).ToString();
Response.Write(strJSON);

//To Deserialize call DeSerializeJSON() method
DeSerializeJSON(strJSON);
}

 

OUTPUT


How to deserialize JSON data back to employee object


To deserialize JSON data back to list of Employees object we have DeSerializeJSON() method below.
 

private void DeSerializeJSON(string strJSON)
    {
        List<Employee> objEmployeeListToDeSerialize = new List<Employee>();
        JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();

        //Deserialize method of JavaScriptSerializer class converts JSON data back to Employee class object
        objEmployeeListToDeSerialize = objJSSerializer.Deserialize<List<Employee>>(strJSON);

        Response.Write("<br/>-------------------------------------------------------<br/>");
        Response.Write("After deserialization<br/>");
        Response.Write("<br/>-------------------------------------------------------<br/>");
        Response.Write("EmpName : " + objEmployeeListToDeSerialize[0].EmpName + "<br/>");
        Response.Write("Country : " + objEmployeeListToDeSerialize[0].Country + "<br/>");
        Response.Write("Project : " + objEmployeeListToDeSerialize[0].Project + "<br/>");
        Response.Write("Skills[0] : " + objEmployeeListToDeSerialize[0].Skills[0].ToString() + "<br/>");
        Response.Write("EmpName : " + objEmployeeListToDeSerialize[1].EmpName + "<br/>");
        Response.Write("Country : " + objEmployeeListToDeSerialize[1].Country + "<br/>");
        Response.Write("Project : " + objEmployeeListToDeSerialize[1].Project + "<br/>");
        Response.Write("Skills[0] : " + objEmployeeListToDeSerialize[1].Skills[0].ToString() + "<br/>");
    }


You can see above after deserializing we got the same object.

OUTPUT

 

Hope this will help. Enjoy Reading......

 

About Author

About Author He is a .Net developer having 4+ years of experience. He loves writting technical article for .Net developers. Najmul Hoda
No Photo
 
Country India
Company BirlaSoft
Home Page www.dotnetlogix.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by