使用JSON.net进行反序列化

3

我对JSON、JSON.net等都非常陌生。在阅读了类似的问题后,我的代码仍然无法正常工作。

我的错误是什么?我忽略了什么?

为测试目的跳过“links”和“meta”类是否可能,还是必须定义每个属性?

我得到了以下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

当我尝试反序列化时,我得到了“nothing”,就像“CoolOutput”一样。
Dim CoolOutput As New clsWerteliste
CoolOutput = JsonConvert.DeserializeObject(Of clsWerteliste)(jsonstring)

3
您可以在Visual Studio中为自己创建类,只需复制JSON并转到“编辑/特殊粘贴/将JSON作为类粘贴”。 - Pikoh
粘贴特殊功能非常有用,谢谢。 - Stefan Meyer
1个回答

4

你的类很接近,看起来你可能试图将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会为您创建类:
Edit菜单 -> Paste Special -> Paste Json As Classes
如果有数组/列表属性,则有时需要调整类。例如,机器人可能会写下:
Public Property elements() As Element

应该是这样的:

Public Property elements As Element()

容器类展示了如何使用<JsonProperty("pname")>来更改属性名称(如果您希望这样做)。这通常需要为VB中的关键字(ReturnError等)创建一个属性名称的别名。在这种情况下,我将codeslinks更改为与您一样的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

Result:

6
TITLE 1
TITLE 2
TITLE 3


2
那个简直太好用了,谢谢。Paste Special 也是一个非常实用的技巧。 - Stefan Meyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接