top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Serialize a Collection in VB.NET using Json.NET ?

0 votes
391 views
How to Serialize a Collection in VB.NET using Json.NET ?
posted May 25, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

For example , assume that you want to serialize the employee VB.NET class as shown below.

Public Class Employee
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property IsPermanent() As Boolean
        Get
            Return m_IsPermanent
        End Get
        Set
            m_IsPermanent = Value
        End Set
    End Property
    Private m_IsPermanent As Boolean
    ' Employee can belong to multiple departments
    Public Property Departments() As List(Of String)
        Get
            Return m_Departments
        End Get
        Set
            m_Departments = Value
        End Set
    End Property
    Private m_Departments As List(Of String)
End Class

Dim employees As New List(Of Employee)()
'Employee 1 Data
Dim emp1 As New Employee()
emp1.Name = "Employee 1"
emp1.IsPermanent = True
emp1.Departments = New List(Of String)() From { _
"Technology", _
"Design" _
}
employees.Add(emp1)
' Employee 2 Data
Dim emp2 As New Employee()
emp2.Name = "Employee 2"
emp2.IsPermanent = False
emp2.Departments = New List(Of String)() From { _
"Technology" _
}
employees.Add(emp2)

Once the List data is available , pass it to the JsonConvert.Serialize method as shown below.

' Convert the collection to Json string
Dim jsonData As String = JsonConvert.SerializeObject(employees)

Below is the complete code snippet that is used in this blog post for serializing the collection in VB.NET.

Imports Newtonsoft.Json

Module Module1

    Sub Main()
        Dim jsonData = SerializeCollection()
        Console.WriteLine("Serialized Data : " & vbLf + jsonData)
        Console.ReadLine()
    End Sub
    Public Function SerializeCollection() As String
        Dim employees As New List(Of Employee)()
        'Employee 1 Data
        Dim emp1 As New Employee()
        emp1.Name = "Employee 1"
        emp1.IsPermanent = True
        emp1.Departments = New List(Of String)() From { _
            "Technology", _
            "Design" _
        }
        employees.Add(emp1)
        ' Employee 2 Data
        Dim emp2 As New Employee()
        emp2.Name = "Employee 2"
        emp2.IsPermanent = False
        emp2.Departments = New List(Of String)() From { _
            "Technology" _
        }
        employees.Add(emp2)
        ' Convert the collection to Json string
        Dim jsonData As String = JsonConvert.SerializeObject(employees)
        Return jsonData
    End Function



End Module
Public Class Employee
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property IsPermanent() As Boolean
        Get
            Return m_IsPermanent
        End Get
        Set
            m_IsPermanent = Value
        End Set
    End Property
    Private m_IsPermanent As Boolean
    ' Employee can belong to multiple departments
    Public Property Departments() As List(Of String)
        Get
            Return m_Departments
        End Get
        Set
            m_Departments = Value
        End Set
    End Property
    Private m_Departments As List(Of String)
End Class
answer May 25, 2016 by Shivaranjini
...