gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:16:01 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com