当前位置: 首页 > 知识库问答 >
问题:

如何在 VB.Net 中使用反序列化的 JSON 数据?

司英彦
2023-03-14

我正在尝试访问我认为我已经从 vb.net 年的 JSON 文件中成功反序列化的数据。

我的程序当前将数据从 JSON 文件读入字符串。我一直在研究如何将 JSON 数据反序列化为类结构,并使用可视化工作室功能为我创建类结构。

我的问题是,反序列化后如何访问该数据?我已经浏览了数百页的论坛和堆栈溢出的帖子,但都无济于事。

以下是JSON数据:

{
  "total": 389097,
  "offset": 0,
  "items": [
    {
      "entity": "Play",
      "arid": "mpdwyOB32n",
      "played_time": "2020-02-21T06:21:26+0000",
      "service_id": "triplej",
      "recording": {
        "entity": "Recording",
        "arid": "mtNYq18DdY",
        "title": "Supalonely",
        "metadata": null,
        "description": null,
        "duration": 219,
        "artists": [
          {
            "entity": "Artist",
            "arid": "ma35vZnl5B",
            "name": "Benee",
            "artwork": [],
            "links": [
              {
                "entity": "Link",
                "arid": "mlb5b4r7A0",
                "url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
                "id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
                "title": "MusicBrainz artist",
                "mini_synopsis": null,
                "short_synopsis": null,
                "medium_synopsis": null,
                "type": "service",
                "provider": "musicbrainz",
                "external": true
              }
            ],
            "is_australian": null,
            "type": "primary",
            "role": null
          }
        ],
        "releases": [
          {
            "entity": "Release",
            "arid": "mr45LNQePX",
            "title": "Stella & Steve",
            "format": "EP",
            "artwork": [],
            "links": [
              {
                "entity": "Link",
                "arid": "mlV0JZbO20",
                "url": "http://musicbrainz.org/release-group/9d0271c7-6448-4cbf-a335-0221e34a73ae",
                "id_component": "9d0271c7-6448-4cbf-a335-0221e34a73ae",
                "title": "MusicBrainz release-group",
                "mini_synopsis": null,
                "short_synopsis": null,
                "medium_synopsis": null,
                "type": "service",
                "provider": "musicbrainz",
                "external": true
              }
            ],
            "artists": [
              {
                "entity": "Artist",
                "arid": "ma35vZnl5B",
                "name": "Benee",
                "artwork": [],
                "links": [
                  {
                    "entity": "Link",
                    "arid": "mlb5b4r7A0",
                    "url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
                    "id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
                    "title": "MusicBrainz artist",
                    "mini_synopsis": null,
                    "short_synopsis": null,
                    "medium_synopsis": null,
                    "type": "service",
                    "provider": "musicbrainz",
                    "external": true
                  }
                ],
                "is_australian": null,
                "type": "primary",
                "role": null
              }
            ],
            "record_label": null,
            "release_year": null,
            "release_album_id": null
          }
        ],
        "artwork": [],
        "links": [
          {
            "entity": "Link",
            "arid": "mlwkZWz960",
            "url": "http://musicbrainz.org/recording/2772cd61-892c-47ab-bc0e-1ca775ebe6f4",
            "id_component": "2772cd61-892c-47ab-bc0e-1ca775ebe6f4",
            "title": "MusicBrainz recording",
            "mini_synopsis": null,
            "short_synopsis": null,
            "medium_synopsis": null,
            "type": "service",
            "provider": "musicbrainz",
            "external": true
          }
        ]
      },
      "release": {
        "entity": "Release",
        "arid": "mr45LNQePX",
        "title": "Stella & Steve",
        "format": "EP",
        "artwork": [],
        "links": [
          {
            "entity": "Link",
            "arid": "mlV0JZbO20",
            "url": "http://musicbrainz.org/release-group/9d0271c7-6448-4cbf-a335-0221e34a73ae",
            "id_component": "9d0271c7-6448-4cbf-a335-0221e34a73ae",
            "title": "MusicBrainz release-group",
            "mini_synopsis": null,
            "short_synopsis": null,
            "medium_synopsis": null,
            "type": "service",
            "provider": "musicbrainz",
            "external": true
          }
        ],
        "artists": [
          {
            "entity": "Artist",
            "arid": "ma35vZnl5B",
            "name": "Benee",
            "artwork": [],
            "links": [
              {
                "entity": "Link",
                "arid": "mlb5b4r7A0",
                "url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
                "id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
                "title": "MusicBrainz artist",
                "mini_synopsis": null,
                "short_synopsis": null,
                "medium_synopsis": null,
                "type": "service",
                "provider": "musicbrainz",
                "external": true
              }
            ],
            "is_australian": null,
            "type": "primary",
            "role": null
          }
        ],
        "record_label": null,
        "release_year": null,
        "release_album_id": null
      }
    }
  ]
}

下面是Visual Studio生成的类结构:

Public Class JSON_Class

    Public Class Rootobject
        Public Property total As Integer
        Public Property offset As Integer
        Public Property items() As Item
    End Class

    Public Class Item
        Public Property entity As String
        Public Property arid As String
        Public Property played_time As Date
        Public Property service_id As String
        Public Property recording As Recording
        Public Property release As Release1
    End Class

    Public Class Recording
        Public Property entity As String
        Public Property arid As String
        Public Property title As String
        Public Property metadata As Object
        Public Property description As Object
        Public Property duration As Integer
        Public Property artists() As Artist
        Public Property releases() As Release
        Public Property artwork() As Object
        Public Property links() As Object
    End Class

    Public Class Artist
        Public Property entity As String
        Public Property arid As String
        Public Property name As String
        Public Property artwork() As Object
        Public Property links() As Object
        Public Property is_australian As Object
        Public Property type As String
        Public Property role As Object
    End Class

    Public Class Release
        Public Property entity As String
        Public Property arid As String
        Public Property title As String
        Public Property format As String
        Public Property artwork() As Object
        Public Property links() As Object
        Public Property artists() As Artist1
        Public Property record_label As Object
        Public Property release_year As Object
        Public Property release_album_id As Object
    End Class

    Public Class Artist1
        Public Property entity As String
        Public Property arid As String
        Public Property name As String
        Public Property artwork() As Object
        Public Property links() As Object
        Public Property is_australian As Object
        Public Property type As String
        Public Property role As Object
    End Class

    Public Class Release1
        Public Property entity As String
        Public Property arid As String
        Public Property title As String
        Public Property format As String
        Public Property artwork() As Object
        Public Property links() As Object
        Public Property artists() As Artist2
        Public Property record_label As Object
        Public Property release_year As Object
        Public Property release_album_id As Object
    End Class

    Public Class Artist2
        Public Property entity As String
        Public Property arid As String
        Public Property name As String
        Public Property artwork() As Object
        Public Property links() As Object
        Public Property is_australian As Object
        Public Property type As String
        Public Property role As Object
    End Class

End Class

我使用下面这行代码反序列化我的数据:(其中JSON_Class是类结构,New_JSON_Content是原始JSON)

Dim JSON_Wrapper = JsonConvert.DeserializeObject(Of JSON_Class)(New_JSON_Content)

如果说我想从填充的类结构中访问任何一个特定变量,我的下一步是什么?谢谢!

共有1个答案

公孙琛
2023-03-14

您看不到任何数据的原因是JSON_Class,您要反序列化到的类,不包含任何成员。代码起作用了;它完全按照您告诉它的那样做——从这个JSON字符串创建一个JSON_Class的实例——它什么也不包含。与反序列化类型中的任何内容都不匹配的JSON标记被忽略,在这种情况下,这就是一切。

要实现这一点,请反序列化为<code>JSON_Class。根对象。您也不需要将反序列化类嵌套在包含类中。

 类似资料:
  • 问题内容: 我有以下JSON字符串要反序列化: 我正在使用DataContractJsonSerializer方法。 它由项目数组组成,我找不到使用VB.Net可以反序列化此结构的示例。我具有以下Application类来存储此信息: 问题答案: 我建议你使用过。原因如下: 更快 比简单的序列化需要更多的代码。 您无需将and 属性与 使用此数据类 并使用它反序列化您的: 如果仍要使用,则可以使用

  • 问题内容: 我有此JSON,但无法弄清楚如何将其转换为C#中的对象列表。 这是JSON: 我尝试使用的代码: 无济于事。如何使用Newtonsoft的JSON库(Json.Net)将JSON反序列化为这些对象的列表? 问题答案: 您可以使用

  • 问题内容: 我正在尝试使用Northwind OData服务: http://services.odata.org/V3/OData/OData.svc/Products?$format=json 并将其反序列化为一系列产品: 但是序列化器似乎不喜欢该部分,并且事实是那里有2 条记录(不确定它们是什么)。 是否有捷径可寻? 问题答案: 使用Json.Net

  • 问题内容: 我在反序列化以下json数组时遇到麻烦(对不起,大小): 如果将其粘贴到json-viewer中,则会得到以下结构: 现在,包含具有坐标的数组的数组具有可变大小。所以我想在Java中,整个对象应该是一个数组,其中包含数组的集合,每个数组都包含一个。就像是 但是gson不接受这一点。我收到以下错误消息: 这似乎很奇怪,因为对我来说好像不像一个数组。但这可能使我感到困惑,或多或少地迷路了…

  • 我收到来自第3方服务提供商的JSON响应,其中包含一系列对象。当我尝试使用Jackson api反序列化JSON时。我收到以下异常 我的回答是 我的POJO课是这样的 我正在尝试使用以下代码反序列化JSON 如果我试着去做 它在BEGIN_对象本身失败。 如何使用数组读取和反序列化JSON。我应该编写自己的反序列化器吗? 编辑如果我使用JSON字符串而不是流,那么我就能够取回所有Java对象。但为

  • 问题内容: 我有一些从Web服务返回的JSON数据。JSON是顶级数组: 使用make 可以对数组中包含的数据进行反序列化,但是,我无法让Serde对顶级数组进行反序列化。 我是否缺少某些内容,还是Serde不能对顶级数组进行反序列化? 问题答案: 您可以使用: