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

标签 .net json vb.net serialization

我正在尝试将 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

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

最佳答案

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

试试这些类(class)。

关于.net - 将 JSON 字符串反序列化为 VB.net 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629812/

相关文章:

c++ - 使用 C++ 每秒追加到磁盘上 JSON 文件中的 JSON 数组

json - Golang具有相同json标签名称的多个字段

c# - 我的 BubbleSort 类没有正确计算迭代次数

c# - 为什么类的这是只读的?

c# - 如何密码保护表单但保持信息可见

javascript - 如何在 D3 中访问 JSON 时添加授权 header

c# - 使用 LINQ 进行列表过滤

c# - 如何更改按下键盘按键时的 TrackBar 控件操作?

sql - vb.net sql上次插入的ID

c# - CefSharp-获取HTML元素的值