反序列化JSON字符串VB.net对象字符串、对象、序列化、JSON

2023-09-04 10:59:17 作者:人心太拥挤

我想转换一个JSON字符串转换为VB.net对象来获取方便地访问全部的数据,在JSON字符串。

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.

我的JSON字符串看起来是这样的:

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
    }
  ]
}

现在我想反序列化这个字符串转换为VB.net对象

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

我的类定义是:

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

其中的Visual Studio 2012将自动为我的JSON字符串创建的。

which Visual Studio 2012 automatically created for my JSON String.

我试图反序列化与.JSON解串器:

I tried to deserialize with .JSON Deserializer:

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

和使用JavaScriptSerializer:

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.

我是新来的Visual Basic,所以我读了很多关于JSON和解串器和其他人有这样那样的问题,但大多数解决方案是C#而不是VB.net

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?

推荐答案

我使用转换你的JSON JsonToCSharp ......然后转换的C#来vb.net ...

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.