Program to convert VB.NET class object to JSON using JavaScriptSerializer

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 394
Suppose we have a class object as under

Public Class ZoneMaster
Public Property Code() As String
Get
Return m_Code
End Get
Set
m_Code = Value
End Set
End Property
Private m_Code As String
Public Property CountryId() As String
Get
Return m_CountryId
End Get
Set
m_CountryId = Value
End Set
End Property
Private m_CountryId As String
Public Property CreatedBy() As String
Get
Return m_CreatedBy
End Get
Set
m_CreatedBy = Value
End Set
End Property
Private m_CreatedBy As String
Public Property Id() As String
Get
Return m_Id
End Get
Set
m_Id = Value
End Set
End Property
Private m_Id As String
Public Property LastModifiedBy() As String
Get
Return m_LastModifiedBy
End Get
Set
m_LastModifiedBy = Value
End Set
End Property
Private m_LastModifiedBy As String
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
End Class


And we want to generate the JSON structure for it. So first we need to populate the same with some data as under

Dim objZone As New ZoneMaster() With { _
Key .LastModifiedBy = "Test User", _
Key .CreatedBy = "Test User", _
Key .Name = "East", _
Key .Code = "C123K345", _
Key .CountryId = "12", _
Key .Id = "1" _
}


Then write the below program that will convert VB.NET class object to JSON using JavaScriptSerializer

Dim javaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim jsonString As String = javaScriptSerializer.Serialize(objZone)
Console.WriteLine(jsonString)

Comments or Responses

Login to post response