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   

Introduction

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);
        }

    }

output:

Serialization of list object is : [{"RollNo":1,"Name":"A","Marks":10},
                                                   {"RollNo":2,"Name":"B","Marks":20},
                                                   {"RollNo":3,"Name":"C","Marks":30},
                                                    {"RollNo":4,"Name":"D","Marks":40}]
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.

References

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

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


Comments (22)
 
1. By Barnypok on 27 Dec 2016

FhEGBU http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
2. By Barnypok on 27 Dec 2016

8sN6GV http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
3. By Barnypok on 27 Dec 2016

mnioBD http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
4. By Barnypok on 28 Dec 2016

euODxF http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
5. By Barnypok on 29 Dec 2016

nYCWQC http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
6. By Barnypok on 30 Dec 2016

Hesqw6 http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
7. By Barnypok on 01 Jan 2017

PfiBQW http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
8. By Barnypok on 02 Jan 2017

TMvx0U http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
9. By Barnypok on 02 Jan 2017

x1isO3 http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
10. By Barnypok on 03 Jan 2017

MN7dNI http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
11. By Barnypok on 04 Jan 2017

p2PL3I http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
12. By Barnypok on 04 Jan 2017

r25jnV http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
13. By Barnypok on 04 Jan 2017

Rsg1Xy http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
14. By Barnypok on 05 Jan 2017

tR8Rcx http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
15. By Barnypok on 05 Jan 2017

vRYYj2 http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
16. By Barnypok on 06 Jan 2017

wwlvQR http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
17. By Barnypok on 06 Jan 2017

RMTsaM http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
18. By Barnypok on 06 Jan 2017

sN7xgS http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
19. By Barnypok on 06 Jan 2017

xEs2ae http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
20. By Barnypok on 08 Jan 2017

F9K111 http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
21. By Barnypok on 08 Jan 2017

WD6fVK http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

 
22. By Barnypok on 08 Jan 2017

wZuyEn http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com