我有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.'我被困住了..。有什么想法吗?提前谢谢。
发布于 2021-01-08 01:10:22
要修复此错误,可以将JSON更改为JSON数组(例如,1,2,3),或者通过更改反序列化类型,使其成为可以从JSON对象反序列化的普通.NET类型(例如,不是像integer这样的原始类型,也不是像数组或列表这样的集合类型)。
换句话说,改变
Dim m As IEnumerable(Of Rootobject) = JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res)至
Dim m As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(res)https://stackoverflow.com/questions/65616857
复制相似问题