11

I've got the following JSON string to deserialize:

[{"application_id":"1","application_package":"abc"},{"application_id":"2","application_package":"xyz"}]

I'm using DataContractJsonSerializer method.

It is made up of array of items and I couldn't find an example using VB.Net that can deserialize this structure. I have the following Application class to store this information:

    <DataContract(Namespace:="")> _
    Public Class ApplicationItem

    <DataMember(Name:="application_id")>
    Public Property application_id As String

    <DataMember(Name:="application_package")>
    Public Property application_package As String

    End Class

3 Answers 3

30

Here is the easiest way to deserialize JSON into an object (using .NET 4):

Example JSON:

{
    "dogs":[],
    "chickens":[
        {
            "name":"Macey",
            "eggs":7
        }, 
        {
            "name":"Alfred",
            "eggs":2
        }
    ]
}

VB.NET:

Try
    Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(JSONString)
    Dim a = j("dogs")                   ' returns empty Object() array
    Dim b = j("chickens")(0)            ' returns Dictionary(Of String, Object)
    Dim c = j("chickens")(0)("name")    ' returns String "Macey"
    Dim d = j("chickens")(1)("eggs")    ' returns Integer 2
Catch ex As Exception
    ' in case the structure of the object is not what we expected.
End Try
2
  • 2
    While Abdul's answer is more complete, this is indeed the easiest way to access JSON data in a quick and dirty way (such as a small, predictable set from an API response).
    – msanford
    Commented Jul 4, 2014 at 2:02
  • 2
    Wow this really helped me out, I was looking for something fast and simple and you did it !!! Commented Mar 24, 2015 at 15:58
22

I'd recommend you to use JavaScriptSerializer over DataContractJsonSerializer. The reasons are:

  • JavaScriptSerializer is faster over DataContractJsonSerializer
  • DataContractJsonSerializer requires more code than JavaScriptSerializer for a simple serialization.

You won't need the DataContract and DataMember attribute to use along with JavaScriptSerializer

Use this data class

<Serializable> _
Public Class ApplicationItem
    Public Property application_id() As String
        Get
            Return m_application_id
        End Get
        Set
            m_application_id = Value
        End Set
    End Property
    Private m_application_id As String
    Public Property application_package() As String
        Get
            Return m_application_package
        End Get
        Set
            m_application_package = Value
        End Set
    End Property
    Private m_application_package As String
End Class

And use this to deserialize your jsonText:

Dim jss As New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of List(Of ApplicationItem))(jsonText)

If you still want to use DataContractJsonSerializer, you can use this code below to deserialize:

Dim obj As New List(Of ApplicationItem)()
Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(json))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.[GetType]())
obj = DirectCast(serializer.ReadObject(ms), List(Of ApplicationItem))
ms.Close()
ms.Dispose()

Courtesy: Used Telerik Code Converter

6
  • It works perfectly. Thank you for showing a link to a code converter too.
    – Bih Cheng
    Commented Nov 14, 2011 at 7:10
  • I am not familiar with VB.NET syntax and use it often to convert C# code to VB.NET :) Commented Nov 14, 2011 at 7:12
  • Cool, thanks for the hint! It is really easier to use JavaScriptSerializer
    – Las Ten
    Commented Mar 14, 2013 at 16:15
  • If JavaScriptSerializer is faster thant DataContractJsonSerializer, why does json.net advertise itself as being "50% faster than DataContractJsonSerializer, and 250% faster than JavaScriptSerializer" on their website?
    – mijiturka
    Commented May 22, 2018 at 10:34
  • @mijiturka Even though JSON.NET had acceptable ground back in those days but probably not as much as of today. If I've answered this question today, I'd have used JSON.NET :) Commented May 23, 2018 at 18:27
0

This works for me:

// Get the HttpWebRequest reaponse
string Response = loResponseStream.ReadToEnd();

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(Response);
string carrier = (dict["Response"]["carrier"]);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.