Json.NET & VB.NET – How to Serialize an Collection?

Do you want to serialize an collection in your VB.NET application?. Json.NET supports this functionality with ease.

The Collection can be an Array , Dictionary or List. You need to simply pass collection to the JsonConvert.SerializeObject static method which would serialize the collection and return you the Json string.

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

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

You will create the List<Employee> and fill it with the values as shown below.

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<Employee> 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