simeh simeh - 1 year ago 199
JSON Question JSON Deserialize

I've got the following JSON string to deserialize:


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

Public Property application_id As String

Public Property application_package As String

End Class

Answer Source

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
            Return m_application_id
        End Get
            m_application_id = Value
        End Set
    End Property
    Private m_application_id As String
    Public Property application_package() As String
            Return m_application_package
        End Get
            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))

Courtesy: Used Telerik Code Converter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download