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?
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With