【问题标题】:Deserializing JSON String to VB.net Object将 JSON 字符串反序列化为 VB.net 对象
【发布时间】:2014-01-04 23:00:49
【问题描述】:

我正在尝试将 JSON 字符串转换为 VB.net 对象,以便轻松访问 JSON 字符串中的所有数据。

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

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

我的班级定义是:

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 字符串自动创建的。

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

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

并使用 JavaScriptSerializer:

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

但在这两种情况下,我只能访问“状态”和“计数”,但不能访问“数据”数组。

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

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

【问题讨论】:

  • 试过 Public Property data() As IList(Of Datum) 然后它说:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.IList`1[WoT_Tool.WoT_Tool +Datum]' 因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])

标签: .net json vb.net serialization


【解决方案1】:

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

试试这些课程。

【讨论】:

  • 使用 JSON.net Serializer 它不起作用,但使用 JavascriptSerializer 它是。但随后它说,列表中没有数据并且它是空的。
【解决方案2】:

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

Public Property data() As Datum

到这里:

Public Property data As Datum()

或者,这也可以:

Public Property data As List(Of Datum)

【讨论】:

    猜你喜欢
    • 2015-01-29
    • 2011-11-16
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多