【问题标题】:how to deserialize this json on vb.net?如何在 vb.net 上反序列化这个 json?
【发布时间】:2021-01-07 17:07:15
【问题描述】:

我有 json 格式的数据:

{
   "status":{
      "timestamp":"2021-01-07T17:13:48.471Z",
      "error_code":0,
      "error_message":null,
      "elapsed":12,
      "credit_count":1,
      "notice":null,
      "total_count":4115
   },
   "data":[
      {
         "id":1,
         "name":"Bitcoin",
         "symbol":"BTC",
         "slug":"bitcoin",
         "num_market_pairs":9732,
         "date_added":"2013-04-28T00:00:00.000Z",
         "tags":[
            "mineable",
            "pow",
            "sha-256",
            "store-of-value",
            "state-channels"
         ],
         "max_supply":21000000,
         "circulating_supply":18593700,
         "total_supply":18593700,
         "platform":null,
         "cmc_rank":1,
         "last_updated":"2021-01-07T17:12:02.000Z",
         "quote":{
            "USD":{
               "price":39362.167971369854,
               "volume_24h":78135138852.86674,
               "percent_change_1h":2.61231359,
               "percent_change_24h":12.47756102,
               "percent_change_7d":36.98956944,
               "market_cap":731888342609.2596,
               "last_updated":"2021-01-07T17:12:02.000Z"
            }
         }
      },
      {
         "id":1027,
         "name":"Ethereum",
         "symbol":"ETH",
         "slug":"ethereum",
         "num_market_pairs":5934,
         "date_added":"2015-08-07T00:00:00.000Z",
         "tags":[
            "mineable",
            "pow",
            "smart-contracts"
         ],
         "max_supply":null,
         "circulating_supply":114155463.749,
         "total_supply":114155463.749,
         "platform":null,
         "cmc_rank":2,
         "last_updated":"2021-01-07T17:12:02.000Z",
         "quote":{
            "USD":{
               "price":1261.606649005652,
               "volume_24h":39345516218.36576,
               "percent_change_1h":3.1558102,
               "percent_change_24h":7.80752209,
               "percent_change_7d":71.81090319,
               "market_cap":144019292086.06207,
               "last_updated":"2021-01-07T17:12:02.000Z"
            }
         }
      }
   ]
}

并且,在 vb.net 中,通过特殊粘贴,我得到了这个定义:

Public Class Rootobject
    Public Property status As Status
    Public Property data() As Datum
End Class

Public Class Status
    Public Property timestamp As Date
    Public Property error_code As Integer
    Public Property error_message As Object
    Public Property elapsed As Integer
    Public Property credit_count As Integer
    Public Property notice As Object
    Public Property total_count As Integer
End Class

Public Class Datum
    Public Property id As Integer
    Public Property name As String
    Public Property symbol As String
    Public Property slug As String
    Public Property num_market_pairs As Integer
    Public Property date_added As Date
    Public Property tags() As String
    Public Property max_supply As Integer
    Public Property circulating_supply As Integer
    Public Property total_supply As Integer
    Public Property platform As Object
    Public Property cmc_rank As Integer
    Public Property last_updated As Date
    Public Property quote As Quote
End Class

Public Class Quote
    Public Property USD As USD
End Class

Public Class USD
    Public Property price As Single
    Public Property volume_24h As Single
    Public Property percent_change_1h As Single
    Public Property percent_change_24h As Single
    Public Property percent_change_7d As Single
    Public Property market_cap As Single
    Public Property last_updated As Date
End Class

但是当我尝试使用此命令反序列化时

Dim m As IEnumerable(Of Rootobject) = 
    JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res)

我收到此错误:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object 
    (e.g. {"name":"value"}) into type 
    'System.Collections.Generic.IEnumerable`1[CoinMarketCap.Rootobject]' 
    because the type requires a JSON array (e.g. [1,2,3]) 
    to deserialize correctly. To fix this error either change the JSON to a
    JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a 
    normal .NET type (e.g. not a primitive type like integer, not a
    collection type like an array or List) that can be deserialized from a 
    JSON object. JsonObjectAttribute can also be added to the type to force 
    it to deserialize from a JSON object. Path 'status', line 1, position 10.'

我卡住了...有什么想法吗? 提前致谢。

【问题讨论】:

  • Datum 应该是 data 或者反之亦然,所以它知道它是什么?
  • 更改为公共属性数据作为列表(基准)并且错误继续...
  • 不止一个问题:tagsPublic Property tags As List(Of String)(避免使用这种格式的属性:Public Property tags() As String,因为这会造成 混淆,这是一个字符串,而不是一个数组),circulating_supplymax_supplytotal_supply 是 Double 而不是 Integer 值,您还必须处理空值,所以用 <JsonProperty(NullValueHandling:=NullValueHandling.Ignore)> 装饰。所有 Single 应该是 Double,last_updated 是 DateTimeOffset,而不是 Date...
  • 你还在尝试反序列化为IEnumerable(Of Rootobject)吗?因为那不会发生。如果您更改了代码(修复了类模型定义中的明显错误),请发布更新的代码。假设这是您正在处理的完整 JSON。

标签: arrays json vb.net


【解决方案1】:

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或 将反序列化类型更改为正常的 .NET 类型(例如,不是整数等原始类型,而不是像数组或列表这样的集合类型)可以从 JSON 对象反序列化

换句话说,改变

Dim m As IEnumerable(Of Rootobject) = JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res)

Dim m As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(res)

【讨论】:

  • 感谢您的回复。如果我这样做,我不能为每个结果做一个“for each”。我只显示一个 iine 的结果。我将使用更多数据更新信息。很抱歉
  • 你的 JSON 只有一个根对象,所以通过“他们”来“为每个”是没有意义的。但是,如果您想遍历所有数据项,则可以在之后执行For Each item In m.data
猜你喜欢
  • 1970-01-01
  • 2012-10-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多