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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20629812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:10:22  来源:igfitidea点击:

Deserializing JSON String to VB.net Object

.netjsonvb.netserialization

提问by Azrael

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 字符串转换为 VB.net 对象,以便轻松访问 JSON 字符串中的所有数据。

My JSON String looks like this:

我的 JSON 字符串如下所示:

{
  "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

现在我正在尝试将此字符串反序列化为 VB.net 对象

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.

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

I tried to deserialize with .JSON Deserializer:

我尝试使用 .JSON Deserializer 反序列化:

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

and with JavaScriptSerializer:

并使用 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

我是 Visual Basic 的新手,所以我阅读了很多关于 JSON 和 Deserializer 以及其他有此类问题的人的信息,但大多数解决方案都是针对 C# 而不是针对 VB.net

Any Ideas what I might did wrong?

任何想法我可能做错了什么?

回答by christiandev

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

我使用JsonToCSharp...转换了您的 JSON ,然后将 C# 转换为 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.

试试这些课程。

回答by Brian Rogers

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

你很接近;你的括号放错了地方。在您的Rootobject班级中,更改此行:

Public Property data() As Datum

to this:

对此:

Public Property data As Datum()

Or, this will also work:

或者,这也将起作用:

Public Property data As List(Of Datum)