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

Published on May 3, 2016 by abundantcode

Filed under Json.NET

Last modified May 3, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 4215 times

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

Leave a Comment


x

You might be Interested in these Posts

C# Program to swap two numbers without using temporary variable

Problem Write a program in Visual CSharp to swap two numbers using temporary variable and display t...

C# Program to swap two numbers

Problem Write a program in C# to swap two numbers using temporary variable and display the result i...

C# Program to print the sum of two numbers

Problem Write a program in C# to add two numbers and display the result in copnsole window. How to...