6

I'm trying to convert a JSON String into an VB.net Object to get easy access to alle the data in the JSON String.

My JSON String looks like this:

{
  "status": "ok",
  "count": 4,
  "data": [
    {
      "clan_id": 500002846,
      "nickname": "Azrael",
      "id": 500429872
    },
    {
      "clan_id": null,
      "nickname": "Azrael0",
      "id": 500913252
    },
    {
      "clan_id": 500028112,
      "nickname": "Azrael0313",
      "id": 504109422
    },
    {
      "clan_id": null,
      "nickname": "Azrael7",
      "id": 501594186
    }
  ]
}

Now I'm trying to deserialize this String into an VB.net Object

My class definitions are:

Public Class Rootobject
    Private _data1 As String

    Public Property status As String
    Public Property count As Integer
    Public Property data() As Datum
End Class

Public Class Datum
    Public Property clan_id As Integer?
    Public Property nickname As String
    Public Property id As Integer
End Class

which Visual Studio 2012 automatically created for my JSON String.

I tried to deserialize with .JSON Deserializer:

Dim Testobject As Rootobject _
= Global.Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(JSON_String)

and with JavaScriptSerializer:

Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim Testobject_2 As Rootobject = serializer.Deserialize(Of Rootobject)(JSON_String)

But in both cases I am just able to get access to "status" and "count" but not to the "data" array.

I'm new to Visual Basic, so i read a lot about JSON and Deserializer and other people with this kind of problems, but most solutions are for C# and not for VB.net

Any Ideas what I might did wrong?

1
  • Tried Public Property data() As IList(Of Datum) Then it says: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[WoT_Tool.WoT_Tool+Datum]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) Commented Dec 17, 2013 at 9:31

2 Answers 2

1

I converted your JSON using JsonToCSharp...and then converted the C# to vb.net...

Public Class Datum
    Public Property clan_id() As System.Nullable(Of Integer)
        Get
            Return m_clan_id
        End Get
        Set
            m_clan_id = Value
        End Set
    End Property
    Private m_clan_id As System.Nullable(Of Integer)
    Public Property nickname() As String
        Get
            Return m_nickname
        End Get
        Set
            m_nickname = Value
        End Set
    End Property
    Private m_nickname As String
    Public Property id() As Integer
        Get
            Return m_id
        End Get
        Set
            m_id = Value
        End Set
    End Property
    Private m_id As Integer
End Class

Public Class RootObject
    Public Property status() As String
        Get
            Return m_status
        End Get
        Set
            m_status = Value
        End Set
    End Property
    Private m_status As String
    Public Property count() As Integer
        Get
            Return m_count
        End Get
        Set
            m_count = Value
        End Set
    End Property
    Private m_count As Integer
    Public Property data() As List(Of Datum)
        Get
            Return m_data
        End Get
        Set
            m_data = Value
        End Set
    End Property
    Private m_data As List(Of Datum)
End Class

Give these classes a try.

Sign up to request clarification or add additional context in comments.

1 Comment

With the JSON.net Serializer it's not working but with the JavascriptSerializer it is. But then it says, that there is no data in the list and that it is empty.
0

You're close; you've got your parentheses in the wrong place. In your Rootobject class, change this line:

Public Property data() As Datum

to this:

Public Property data As Datum()

Or, this will also work:

Public Property data As List(Of Datum)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.