使用JSON.net反序列化 [英] Deserialize using JSON.net

查看:83
本文介绍了使用JSON.net反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对json,JSON.net以及其他所有东西都很陌生.在阅读了类似的问题之后,我无法使我的代码正常工作. 我的错误到底是什么?我监督了什么? 是否可以出于测试目的而跳过链接"和元"类,还是必须定义每个属性?

I am very new to json, JSON.net and all that. After reading similiar questions here I cannot get my code working. What exactly is my error? What have I overseen? Is it possible to skip the classes "links" and "meta" for testing purposes or do I have to define EVERY property?

我有以下REST输出:

I have got the following REST output:

{
   "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
   }
}

由于我不太了解,我需要三节课:代码,链接和元.

As I unterstood I need three classes: e.g. codes, links and meta.

我创建了一个类"clscodes":

I created a class "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类:

And I created a 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"一样,我什么也没得到

When I try to deserialize it I get "nothing" as in "CoolOutput"

Dim CoolOutput As New clsWerteliste
CoolOutput = JsonConvert.DeserializeObject(Of clsWerteliste)(jsonstring)

推荐答案

您的类非常接近,看起来您可能尝试了一些修饰,例如将codes更改为Codes,但是这样做属性不再匹配.您可以更改类名,但不能更改属性名(至少不能这样):

Your classes are pretty close, it looks like you possibly tried to pretty things up a bit such as changing codes to Codes but in so doing the properties no longer match. You can change class names but not property names (at least not that way):

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属性,意味着您可以跳过所有GetSet样板代码. VS还将为您创建类:
编辑菜单-> 选择性粘贴-> 将Json粘贴为类

Using AutoImplement properties, available for some time now, means you can skip all the Get, Set boilerplate code. VS will create the classes for you also:
Edit Menu -> Paste Special -> Paste Json As Classes

如果存在数组/列表属性,有时必须调整类.例如,机器人可能会写:

You sometimes have to tweak the class if there is an array/list property. For instance, the robots may write:

Public Property elements() As Element

应该在什么时候出现:

Public Property elements As Element()

如果需要,容器类显示如何使用<JsonProperty("pname")>更改属性名称.通常需要执行此操作以为属性名称创建别名,该属性名称是VB中的关键字(ReturnError等).在这种情况下,我像您一样将codeslinks更改为Lists.

The container class shows how to use <JsonProperty("pname")> to change the property name if you wish. This often needs to be done to create an alias for a property name which is a key word in VB (Return, Error etc). In this case, I changed codes and links to be Lists as you did.

    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
标题1
标题2
标题3

6
TITLE 1
TITLE 2
TITLE 3

这篇关于使用JSON.net反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆