Json.NET & VB.NET – How to Deserialize an Object ?

Published on April 13, 2016 by abundantcode

Filed under Json.NET

Last modified February 13, 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 4505 times

One of the ways to Deserialize an object to JSON string in VB.NET in the Json.NET is using the DeSerializeObject method defined in the JsonConvert method.

How to Deserialize an Object in VB.NET using Json.NET ?

Below is a sample code snippet demonstrating how you can deserialize an object from Json string to VB.NET object using Json.NET. It takes the json string that contains the employee information and deserializes it to the Employee class.

Imports Newtonsoft.Json

Module Module1

    Sub Main()
        Dim jsonTxt As String = "{'Name': 'Abundantcode'," & vbCr & vbLf & "  'IsPermanent': true," & vbCr & vbLf & " 'Departments': [" & vbCr & vbLf & "    'Technology'," & vbCr & vbLf & "    'Product Engineering'" & vbCr & vbLf & "  ]" & vbCr & vbLf & "    }"
        ' Deserialize an Json string to Employee object 
        Dim emp As Employee = JsonConvert.DeserializeObject(Of Employee)(jsonTxt)

    End Sub
    Public Class Employee
    Public Property Name() As String
            Return m_Name
        End Get
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property IsPermanent() As Boolean
            Return m_IsPermanent
        End Get
            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)
            Return m_Departments
        End Get
            m_Departments = Value
        End Set
    End Property
    Private m_Departments As List(Of String)
End Class

End Module

The Deserialized object contains the following data.



If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

C Program to display Inverted half-pyramid using *

Problem Write a program in C to print Inverted half-pyramid using * as shown below.The program shou...

How to use array Rank in C# ?

In C# , the Rank property of the array is is used to get the rank of the array. In simple terms , ra...

Json.NET & Oxygene – How to Serialize a Collection?

Do you want to serialize an collection in your Remobjects Oxygene.NET application?. Json.NET support...