我有一個JSON返回,看起來像這樣:JSON數組的VB.NET JSON陣列 - 反序列化
{"coin1":{"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"},
"coin2":{"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"},
"coin3":{"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"}
}
我想它返回到「coinName」的列表。
我做:
Public Class coinName
Public Vals As cValues
End Class
Public Class cValues
Public available As String
Public onOrders As String
Public btcValue As String
End Class
,我使用下面的代碼進行反序列化:
Dim pData = JsonConvert.DeserializeObject(Of List(Of coinName))(bals)
「BALS」 是一個字符串形式的JSON回報。
我收到以下錯誤:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WindowsApplication21.coinName]' 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<T>) 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 '1CR', line 1, position 7.
任何幫助將不勝感激。
謝謝。
我認爲你的JSON結構不正確。 JSON數組應該如下:''汽車':[「福特」,「寶馬」,「菲亞特」]'你不應該使用** {} ** – Mederic
因此,回到結構,我認爲你仍然缺少** [] **基本上你應該有:''coin3「:[{」available「:」0.00000000「,」onOrders「:」0.00000000「,」btcValue「:」0.00000000「}]'嘗試運行你的代碼更正的JSON看它是否工作。 – Mederic
{「coin1」:[{「available」:「0.00000000」,「onOrders」:「0.00000000」,「btcValue」:「0.00000000」}],「coin2」:[{「available」:「0.00000000」,「onOrders 「:」0.00000000「,」btcValue「:」0.00000000「}],」coin3「:[{」available「:」0.00000000「,」onOrders「:」0.00000000「,」btcValue「:」0.00000000「}]} 不起作用 –