In this article I will explain with an example, what is JavaScriptSerializer and how to use it in ASP.Net 4.5 using C# and VB.Net.
This article makes use of ASP.Net version 4.5.
 
 
What is JavaScriptSerializer
JavaScriptSerializer is a class that helps to serialize and deserialize JSON.
Serialization is the process of converting .Net objects into a JSON format, and deserialization is the process of converting JSON data into .Net objects..
This class belongs to the System.Web.Script.Serialization which belongs to the System.Web.Extensions dll.
JavaScriptSerializer class has the following methods:
Serialize - Serialize method serializes an .Net object and converts it to a JSON formatted string.
Deserialize - Deserialize method converts a JSON formatted string to an .Net object of the specified type.
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Web.Script.Serialization;
 
VB.Net
Imports System.Web.Script.Serialization
 
 
Property Class
The following class consists of two properties.
C#
public class Person
{
    public string Name { get; set; }       
    public int Age { get; set; }
}
 
VB.Net
Public Class Person
    Public Property Name As String
    Public Property Age As Integer
End Class
 
 
Using JavaScriptSerializer in ASP.Net 4.5
Inside the Page_Load event handler, an object of the Person class is created and values of Name and Age properties are set.
Finally, the person object is serialized into a JSON string using the Serialize method of the JavaScriptSerializer class.
C#
protected void Page_Load(object sender, EventArgs e)
{
    Person person = new Person();
    person.Name = "Mudassar Khan";
    person.Age = 35;
    string json = new JavaScriptSerializer().Serialize(person);
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim person As Person = New Person()
    person.Name = "Mudassar Khan"
    person.Age = 35
    Dim json As String = New JavaScriptSerializer().Serialize(person)
End Sub
 
 
Screenshot
What is JavaScriptSerializer and how to use it in ASP.Net 4.5
 
 
Downloads


Other available versions