DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

converting generic list/object to JSON and JSON to list/Object using JavaScriptSerializer in .NET

By Ashok Nalam on 17 Dec 2012 | Category: C# | Tagged: JSON generics list conversion 
In this article we will see how to convert an .NET object or generic list to JSON and JSON to .NET object or list using JavaScriptSerializer class.
  Discuss Add Comment   |  
Add rating Rate this resource   


JavaScriptSerializer provides serialization and deserialization functionality for AJAX based applications. Its basically serilize and deserilize the .NET objects to JSON Format.
JavaScriptSerializer is an inbuilt class from .NET framework class library under system.web.Script.serialization namespace(System.Web.Extensions.dll) and it is avalible from .NET 3.5 version onwards.

In the below example we will try to convert .NET object to JSON using JavaScriptSerializer.Serialize method and JSON to .NET object using JavaScriptSerializer.Deserialize method.

Sample Program:

    public class Student
        public int RollNo { get; set; }
        public string Name { get; set; }
        public int Marks { get; set; }


class DotNETObject2JSON
        static void Main(string[] args)
            //creating sample student ojects
            Student obj1 = new Student { RollNo = 1, Name = "A", Marks = 10 };
            Student obj2 = new Student { RollNo = 2, Name = "B", Marks = 20 };
            Student obj3 = new Student { RollNo = 3, Name = "C", Marks = 30 };
            Student obj4 = new Student { RollNo = 4, Name = "D", Marks = 40 };

            //adding student objects to list
            List<Student> objStudentList = new List<Student>() { obj1, obj2, obj3, obj4 };

            System.Web.Script.Serialization.JavaScriptSerializer objJSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            //Serialization .NET Object to JSON
            string strJSON = objJSSerializer.Serialize(objStudentList);

            //De-serialization JOSON to .NET Object to JSON
            List<Student> listStudent = objJSSerializer.Deserialize<List<Student>>(strJSON);

            //Serialization single student Object to JSON
            string strSingleStudentJSON = objJSSerializer.Serialize(obj1);

            //De-Seralization JOSON to single student Object
            Student listStudent0 = objJSSerializer.Deserialize<Student>(strSingleStudentJSON);



Serialization of list object is : [{"RollNo":1,"Name":"A","Marks":10},
Serialization of student object is: {"RollNo":1,"Name":"A","Marks":10}{"RollNo":1,"Name":"A","Marks":10}

De-Serialization output will be list<student> object and student object.


  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 6833  (Expert)
Location : INDIA
Home Page :
About : I am admin of this site.
Rate this resource
Add your Comment
Name Email WebSite
Captcha Refresh

Comments (5)
1. By JimmiXzSq on 18 May 2017


2. By JimmiXzSq on 18 May 2017


3. By JimmiXzSq on 21 May 2017


4. By JimmiNu on 11 Sep 2017


5. By Billie on 06 Dec 2017

I've only just arrived enthusiasm avanafil cost deny soft "If we ever needed to move to another planet, Mars is probably our best bet," Rushby said in a statement. "It's very close and will remain in the habitable zone until the end of the sun's lifetime 6 billion years from now."