简体   繁体   English

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

[英]Deserializing JSON String to VB.net Object

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 现在我正在尝试将此String反序列化为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反序列化器反序列化:

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. 但在这两种情况下,我只能访问“status”和“count”,但不能访问“data”数组。

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? 任何想法我可能做错了什么?

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. 试试这些课程。

You're close; 你很亲密; you've got your parentheses in the wrong place. 你的括号在错误的地方。 In your Rootobject class, 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM