In this article I will explain with an example, how to convert Class object to JSON string in C# and VB.Net.
This article makes use of .Net version 4.5.
 
 
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
 
 
Converting Class object to JSON string in .Net 4.5
First, 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#
Person person = new Person();
person.Name = "Mudassar Khan";
person.Age = 35;
string json = new JavaScriptSerializer().Serialize(person);
 
VB.Net
Dim person As Person = New Person()
person.Name = "Mudassar Khan"
person.Age = 35
Dim json As String = New JavaScriptSerializer().Serialize(person)
 
 
Screenshot
.Net 4.5: Convert Class object to JSON string in C# and VB.Net
 
 


Other available versions