首页 > 解决方案 > 如何反序列化在 API 调用中收到的具有多个标签的大型 JSON

问题描述

JSON从 API 调用中接收到大量信息。
此示例列出了收到的 2 个项目;通常,此列表将包含数百个。

JSON收到的是 :

{
"data":[
        {
         "id":"2324682","type":"organizations","attributes":
        {
         "psa-integration":null,
         "name":"Client1",
         "alert":null,
         "description":null,
         "organization-type-id":null,
         "organization-type-name":null,
         "organization-status-id":null,
         "organization-status-name":null,
         "my-glue-account-id":null,
         "primary":true,
         "quick-notes":null,
         "short-name":"P",
         "created-at":"2017-09-25T17:21:26.000Z",
         "updated-at":"2018-02-09T17:25:34.000Z",
         "my-glue-account-status":null,
         "logo":"client1.jpg"
         },"relationships":{"adapters-resources":{"data":[]}}},
    {
      "id":"2388378","type":"organizations","attributes":
        {
         "psa-integration":null,
         "name":"Client2",
         "alert":null,
         "description":null,
         "organization-type-id":53460,
         "organization-type-name":"E123",
         "organization-status-id":17054,
         "organization-status-name":"Active",
         "my-glue-account-id":null,
         "primary":false,
         "quick-notes":null,
         "short-name":null,
         "created-at":"2017-10-16T19:36:30.000Z",
         "updated-at":"2018-08-21T19:06:47.000Z",
         "my-glue-account-status":null,
         "logo":"client2.jpg"
          },"relationships":{"adapters-resources":{"data":[]}}}],
"meta":
     {
      "current-page":1,
      "next-page":2,
      "prev-page":null,
      "total-pages":16,
      "total-count":31,
      "filters":
       {
        "id":{"permitted-values":[]},
        "name":{"permitted-values":[]},
        "organization-type-id":{"permitted-values": 
[{"value":39230,"data":{"name":"client"}},{"value":39231,"data":    {"name":"CLient2"}},{"value":39232,"data":{"name":"Internal"}},    {"value":39233,"data":{"name":"Other"}},{"value":39234,"data":{"name":"Partner"}},{"value":39235,"data":{"name":"Prospect"}},{"value":39236,"data":{"name":"Vendor"}},{"value":53460,"data":{"name":"newname"}}]},"organization-status-id":{"permitted-values":[{"value":17054,"data":{"name":"Active"}},{"value":17055,"data":{"name":"Inactive"}}]},"created-at":{"permitted-values":[]},"updated-at":{"permitted-values":[]},"my-glue-account-id":{"permitted-values":[]}}},"links":{"self":"https://api.itglue.com/organizations?page%5Bnumber%5D=1\u0026page%5Bsize%5D=2\u0026sort=id","next":"https://api.itglue.com/organizations?page%5Bnumber%5D=2\u0026page%5Bsize%5D=2\u0026sort=id","last":"https://api.itglue.com/organizations?page%5Bnumber%5D=16\u0026page%5Bsize%5D=2\u0026sort=id"}}

到目前为止,我已经尝试为数据创建类,但它似乎没有给我任何信息。

为了处理数据,我不得不更改所有属性名称,因为很多都包含破折号:
例如,Created-atis nowCreated_at

我曾经jsonutils.com创建过 VB.Net 类。

我将 加载JSON到一个名为Change_String.

如果有人能解释我做错了什么,我将不胜感激

Dim changed_string as string = "JSON INPUT _ instead of -"   
Dim obj As Attributes
obj = JsonConvert.DeserializeObject(Of Attributes)(changed_string)
msgbox(obj.name)

课程:

Public Class Attributes
    Public Property psa_integration As Object
    Public Property name As String
    Public Property alert As Object
    Public Property description As Object
    Public Property organization_type_id As Integer?
    Public Property organization_type_name As String
    Public Property organization_status_id As Integer?
    Public Property organization_status_name As String
    Public Property my_glue_account_id As Object
    Public Property primary As Boolean
    Public Property quick_notes As String
    Public Property short_name As String
    Public Property created_at As String
    Public Property updated_at As String
    Public Property my_glue_account_status As Object
    Public Property logo As String
End Class

Public Class AdaptersResources
    Public Property data As Object()
End Class

Public Class Relationships
    Public Property adapters_resources As AdaptersResources
End Class

Public Class Datum
    Public Property id As String
    Public Property type As String
    Public Property attributes As Attributes
    Public Property relationships As Relationships
End Class

Public Class Id
    Public Property permitted_values As Object()
End Class

Public Class Name
    Public Property permitted_values As Object()
End Class

Public Class Data
    Public Property name As String
End Class

Public Class PermittedValue
    Public Property value As Integer
    Public Property data As Data
End Class

Public Class OrganizationTypeId
    Public Property permitted_values As PermittedValue()
End Class

Public Class OrganizationStatusId
    Public Property permitted_values As PermittedValue()
End Class

Public Class CreatedAt
    Public Property permitted_values As Object()
End Class

Public Class UpdatedAt
    Public Property permitted_values As Object()
End Class

Public Class MyGlueAccountId
    Public Property permitted_values As Object()
End Class

Public Class Filters
    Public Property id As Id
    Public Property name As Name
    Public Property organization_type_id As OrganizationTypeId
    Public Property organization_status_id As OrganizationStatusId
    Public Property created_at As CreatedAt
    Public Property updated_at As UpdatedAt
    Public Property my_glue_account_id As MyGlueAccountId
End Class

Public Class Meta
    Public Property current_page As Integer
    Public Property next_page As Object
    Public Property prev_page As Object
    Public Property total_pages As Integer
    Public Property total_count As Integer
    Public Property filters As Filters
End Class

Public Class Links
End Class

Public Class Organizations
    Public Property data As Datum()
    Public Property meta As Meta
    Public Property links As Links
End Class

标签: vb.netjson.net

解决方案


我又看了一遍。
在线服务生成的课程并不十分壮观。

这是一个修改后的类结构,应该提供正确的结果。
我将所有内容压缩在一个名为 的类JSON_Organizations中,其中包含 JSON 反序列化器所需的所有类。

所有属性都使用JSON Attributes重命名,因此会处理坏名。

示例转换器:

Imports Newtonsoft.Json

Dim JSONObject As String = "[The JSON Object]"

Dim Organizations As JSON_Organizations.RootObject = New JSON_Organizations.RootObject
Organizations = JsonConvert.DeserializeObject(Of JSON_Organizations.RootObject)(JSONObject)

JSON_Organizations容器类 :

Imports Newtonsoft.Json

Public Class JSON_Organizations

    Public Class RootObject

        <JsonProperty("data")>
        Public Property Data As Datum()

        <JsonProperty("meta")>
        Public Property Meta As Meta

        <JsonProperty("links")>
        Public Property Links As Links
    End Class

    Public Class Datum

        <JsonProperty("id")>
        Public Property Id As String

        <JsonProperty("type")>
        Public Property Type As String

        <JsonProperty("attributes")>
        Public Property Attributes As Attributes

        <JsonProperty("relationships")>
        Public Property Relationships As Relationships
    End Class

    Public Class Attributes

        <JsonProperty("psa-integration")>
        Public Property PsaIntegration As Object

        <JsonProperty("name")>
        Public Property Name As String

        <JsonProperty("alert")>
        Public Property Alert As Object

        <JsonProperty("description")>
        Public Property Description As Object

        <JsonProperty("organization-type-id")>
        Public Property OrganizationTypeId As Integer?

        <JsonProperty("organization-type-name")>
        Public Property OrganizationTypeName As String

        <JsonProperty("organization-status-id")>
        Public Property OrganizationStatusId As Integer?

        <JsonProperty("organization-status-name")>
        Public Property OrganizationStatusName As String

        <JsonProperty("my-glue-account-id")>
        Public Property MyGlueAccountId As Object

        <JsonProperty("primary")>
        Public Property Primary As Boolean

        <JsonProperty("quick-notes")>
        Public Property QuickNotes As Object

        <JsonProperty("short-name")>
        Public Property ShortName As String

        <JsonProperty("created-at")>
        Public Property CreatedAt As DateTimeOffset

        <JsonProperty("updated-at")>
        Public Property UpdatedAt As DateTimeOffset

        <JsonProperty("my-glue-account-status")>
        Public Property MyGlueAccountStatus As Object

        <JsonProperty("logo")>
        Public Property Logo As String
    End Class

    Public Class Relationships

        <JsonProperty("adapters-resources")>
        Public Property AdaptersResources As AdaptersResources
    End Class

    Public Class AdaptersResources

        <JsonProperty("data")>
        Public Property Data As List(Of Object)
    End Class

    Public Class Links

        <JsonProperty("self")>
        Public Property Self As String

        <JsonProperty("next")>
        Public Property NextLink As String

        <JsonProperty("last")>
        Public Property Last As String
    End Class

    Public Class Meta

        <JsonProperty("current-page")>
        Public Property CurrentPage As Long

        <JsonProperty("next-page")>
        Public Property NextPage As Long

        <JsonProperty("prev-page")>
        Public Property PrevPage As Object

        <JsonProperty("total-pages")>
        Public Property TotalPages As Long

        <JsonProperty("total-count")>
        Public Property TotalCount As Long

        <JsonProperty("filters")>
        Public Property Filters As Filters
    End Class

    Public Class Filters

        <JsonProperty("id")>
        Public Property Id As CreatedAt

        <JsonProperty("name")>
        Public Property Name As CreatedAt

        <JsonProperty("organization-type-id")>
        Public Property OrganizationTypeId As CreatedAt

        <JsonProperty("organization-status-id")>
        Public Property OrganizationStatusId As CreatedAt

        <JsonProperty("created-at")>
        Public Property CreatedAt As CreatedAt

        <JsonProperty("updated-at")>
        Public Property UpdatedAt As CreatedAt

        <JsonProperty("my-glue-account-id")>
        Public Property MyGlueAccountId As CreatedAt
    End Class

    Public Class CreatedAt

        <JsonProperty("permitted-values")>
        Public Property PermittedValues As List(Of PermittedValue)
    End Class

    Public Class PermittedValue

        <JsonProperty("value")>
        Public Property Value As Integer

        <JsonProperty("data")>
        Public Property Data As Data
    End Class

    Public Class Data
        <JsonProperty("name")>
        Public Property Name As String
    End Class
End Class

推荐阅读