繁体   English   中英

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

[英]How do I use Deserialised JSON Data In VB.Net?

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

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

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

这是 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)

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

您看不到任何数据的原因是您要反序列化的类JSON_Class不包含任何成员。 代码有效; 它完全按照你的要求去做——从这个 JSON 字符串创建一个JSON_Class的实例——它不包含任何内容。 与反序列化类型中的任何内容都不匹配的 JSON 令牌将被忽略,在这种情况下就是一切。

要使其工作,请反序列化为JSON_Class.Rootobject 您也不需要将反序列化类嵌套在包含类中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM