反序列化嵌套的 JSON 和 VB.Net [英] Deserialize nested JSON and VB.Net

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

问题描述

我尝试从以下 json 结果中的基本部分获取值

I try to get the values from the basic section in the following json result

   {
  "responseCode": "Ok",
  "responseMessage": "",
  "ssnStatus": "Active",
  "basic": {
    "firstName": "Testnamn",
    "givenName": "Gettnamn",
    "surName": "Testname",
    "middleName": null,
    "lastName": "Lastname",
    "co": null,
    "street": "Teststreet lgh 1202",
    "zipCode": "92609",
    "city": "Stad"
  },
  "phoneNumbers": {
    "phoneNumbers": []
  },
  "ssnStatusBlock": null
}

我可以使用下面的代码获得第一级 (ssnStatus),但是如何获得 firstName、givenName 等?

I can get the first level (ssnStatus) with the code below, but how do I get the firstName, givenName etc?

Dim post As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
                    Dim ssnStatus As String = post.ssnStatus

Public Class Post
        Public Property ssnStatus As String
End Class

推荐答案

您缺少 JSON 对象的所有其他成员的属性和类定义.

You are missing the properties and classes definition for all the other membersof the JSON object.

  • 在您的项目中创建一个新的类文件.给它一个正确描述 JSON 用法的名称,
  • 添加Imports Newtonsoft.Json 导入,
  • 复制一个描述 JSON 结构的 JSON 对象的示例(你这里有一个很好),
  • 将插入符号放置在新的类定义中,
  • 查看 Visual Studio 菜单:Edit ->粘贴特殊 ->将 JSON 粘贴为类
  • Create a new class file in your Project. Give it a name that properly describe the JSON usage,
  • add the Imports Newtonsoft.Json import,
  • copy a sample of the JSON object that describes the JSON structure (the one you have here is good),
  • position the caret inside the new class definition,
  • see the Visual Studio menu: Edit -> Paste Special -> Paste JSON as classes

Visual Studio 将创建处理您选择的 JSON 对象所需的所有类和属性.

Visual Studio will create all the classes and properties needed to handle the JSON object you selected.

注意:对于更复杂的类,Visual Studio 生成的结果可能不完全符合您的要求.在这种情况下,请尝试提供免费转换服务的专业网站之一:

Note: With more complex classes, it may happen that the result Visual Studio produces is not exactly what you require. In this case, try one of the specialized WebSites that provide a free conversion service:

  • Json Utils (VB.Net, C#, Java, Javascript, more...)
  • QuickType (C#, C++, Java, Javascript, Python, Go, more...)
  • json2csharp (C#)
  • JSON Formatter (JSON formatting and validation)

新的根类定义将命名为Rootobject.根据需要更改此名称,
更清楚地说明该类的用途.

The new root class definition will be named Rootobject. Change this name as needed,
to make it more clear what the class is used for.

这是 Visual Studio 使用您问题中的 JSON 对象创建的类定义.
我创建了一个名为 MyWebSitePost 的类 Project 类,如前所述创建了 JSON 对象类定义,然后我重命名了默认主类 Post,替换默认的 Rootobject 名称:

This is the class definition that Visual Studio creates with the JSON object in your question.
I created a class Project class named MyWebSitePost, created the JSON bject class definition as previously described, I then renamed the default master class Post, replacing the default Rootobject name:

Public Class MyWebSitePost

    Public Class Post
        Public Property responseCode As String
        Public Property responseMessage As String
        Public Property ssnStatus As String
        Public Property basic As Basic
        Public Property phoneNumbers As Phonenumbers
        Public Property ssnStatusBlock As Object
    End Class

    Public Class Basic
        Public Property firstName As String
        Public Property givenName As String
        Public Property surName As String
        Public Property middleName As Object
        Public Property lastName As String
        Public Property co As Object
        Public Property street As String
        Public Property zipCode As String
        Public Property city As String
    End Class

    Public Class Phonenumbers
        Public Property phoneNumbers() As Object
    End Class
End Class

然后您可以使用您已有的代码来访问所有其他属性:

You can then use the code you already have to access all the other properties:

(某些属性类型可能已设置为Object;修改为必需的).

(Some Properties type may have been set to Object; modify as required).

Dim JsonPost As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
Dim ssnStatus As String = JsonPost.ssnStatus
Dim FirstName As String = JsonPost.basic.firstName

等等.

注意:
如您所见,所有属性都具有默认名称,如 JSON 对象中所定义.一些属性名称并不真正适合描述它们的内容.例如,co 属性可能是 Country 名称.要更改属性名称,您可以使用 <JsonProperty> 属性,该属性引用 JSON 对象原始名称并为该属性使用自定义名称:

Note:
As you can see, all properties have the default name, as defined in the JSON object. Some properties names are not really adeguate to describe thier content. For example the co property is probably the Country name. To change the property name, you can use the <JsonProperty> attribute, which references the JSON object original name and use a custom name for the Property:

'(...)
<JsonProperty("co")>
Public Property Country As Object
'(...)

然后您可以使用自定义名称访问此属性:

You can then access this Property using the custom name:

Dim CountryName As String = JsonPost.basic.Country

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

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