我对 json、JSON.net 和所有这些东西都很陌生。在这里阅读类似的问题后,我无法让我的代码工作。 我的错误到底是什么?我监督了什么? 是否可以出于测试目的跳过类“链接”和“元”,或者我是否必须定义每个属性?
我得到了以下 REST 输出:
{
"codes" : [
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/4Sxnr961xzM",
"rel" : "document_field_definition_code",
"title" : "TITLE 1"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/buho0CsLc5k",
"rel" : "document_field_definition_code",
"title" : "TITLE 2"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/RvQoykUM_Sk",
"rel" : "document_field_definition_code",
"title" : "TITLE 3"
}
],
"links" : [
{
"about" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes?about=1",
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "GET",
"rel" : "self",
"title" : null,
"type" : "codes"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "POST",
"rel" : "codes",
"title" : "create new codes entity"
}
],
"meta" : {
"description" : null,
"last_page" : 1,
"page_offset" : 0,
"page_size" : 50,
"query-template" : "/codes{?query_search,page_offset,page_size,query_identification,embedded,properties,about}",
"total" : 6
}
}
据我所知,我需要三个类:例如代码、链接和元数据。
我创建了一个类“clscodes”:
Public Class clsCode
Private m_href As String
Private m_rel As String
Private m_title As String
Public Property Href As String
Get
Return m_href
End Get
Set(value As String)
m_href = value
End Set
End Property
Public Property Rel As String
Get
Return m_rel
End Get
Set(value As String)
m_rel = value
End Set
End Property
Public Property Title As String
Get
Return m_title
End Get
Set(value As String)
m_title = value
End Set
End Property
End Class
然后我创建了一个类 clsValuelist:
Public Class clsWerteliste
Private m_code As IList(Of clsCode)
Public Property Code() As clsCode()
Get
Return m_code
End Get
Set(value As clsCode())
m_code = value
End Set
End Property
End Class
当我尝试反序列化它时,我得到的是“CoolOutput”中的“无”
Dim CoolOutput As New clsWerteliste
CoolOutput = JsonConvert.DeserializeObject(Of clsWerteliste)(jsonstring)
最佳答案
你的类(class)非常接近,看起来你可能试图美化一些东西,比如改变 codes
至 Codes
但这样做的属性不再匹配。您可以更改类名但不能更改属性名(至少不能那样):
Public Class CodeLinkContainer
<JsonProperty("codes")>
Public Property Codes As IList(Of Code)
<JsonProperty("links")>
Public Property Links As IList(Of Link)
<JsonProperty("meta")>
Public Property Meta As Meta
End Class
Public Class Meta
Public Property description As Object
Public Property last_page As Integer
Public Property page_offset As Integer
Public Property page_size As Integer
Public Property querytemplate As String
Public Property total As Integer
End Class
Public Class Code
Public Property href As String
Public Property rel As String
Public Property title As String
End Class
Public Class Link
Public Property about As String
Public Property href As String
Public Property method As String
Public Property rel As String
Public Property title As String
Public Property type As String
End Class
使用现在可用的 AutoImplement 属性意味着您可以跳过所有 Get
, Set
样板代码。 VS 也会为您创建类:
编辑菜单 -> 选择性粘贴 -> 将 Json 粘贴为类
如果有数组/列表属性,有时您必须调整类。例如,机器人可能会写:
Public Property elements() As Element
什么时候应该:
Public Property elements As Element()
容器类展示了如何使用<JsonProperty("pname")>
如果您愿意,可以更改属性名称。这通常需要为属性名称创建别名,这是 VB 中的关键字(Return
、Error
等)。在这种情况下,我更改了 codes
和 links
成为Lists
和你一样。
Dim jstr = ... from whereever
Dim CodeLinks = JsonConvert.DeserializeObject(Of CodeLinkContainer)(jstr)
Console.WriteLine(CodeLinks.meta.total)
For Each Item In CodeLinks.codes
Console.WriteLine(Item.title)
Next
结果:
6
TITLE 1
TITLE 2
TITLE 3
关于.net - 使用 JSON.net 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42088220/