“节点必须是 'JsonObject' 类型。”
'The node must be of type 'JsonObject'.'
我正在尝试解析 this Json
使用代码:
Dim streamData As Stream = Nothing
Using http As New HttpClient
Dim url As String = "https://api.hotbit.io/api/v1/market.deals?market=KIBA/USDT&limit=150&last_id=1521100930&api_key=44812d8f-66d3-01c0-94c3b29305040b03&sign=98EEC3D69D3F70F9BDFED901984B2AA4"
Dim t As Task(Of Stream) = http.GetStreamAsync(url)
streamData = t.Result
End Using
Dim jsonResponse As JsonNode = JsonNode.Parse(streamData)
Dim result As JsonObject = jsonResponse("result").AsObject
Dim c As String = String.Empty
For Each kvp In result.AsEnumerable
c &= kvp.Value("price").ToString & vbCr
Next
RichTextBox1.Text = c
End Sub
但我在调试时一直收到错误
The node must be of type 'JsonObject'.'
on the line
Dim result As JsonObject = jsonResponse("result").AsObject
如果我已经在尝试将其解析为 Jsonobject,怎么会出现错误?
谢谢
试一试..
- 将其粘贴到新的 class 文件中:
Namespace HotBit
Partial Public Class TradeCollection
<JsonProperty("error")>
Public Property [Error] As Object
<JsonProperty("result")>
Public Property Result As List(Of Result)
<JsonProperty("id")>
Public Property Id As Long
End Class
Partial Public Class Result
<JsonProperty("id")>
Public Property Id As Long
<JsonProperty("time")>
Public Property Time As Long
<JsonProperty("price")>
Public Property Price As String
<JsonProperty("amount")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property Amount As Long
<JsonProperty("type")>
Public Property Type As TypeEnum
End Class
Public Enum TypeEnum
Buy
Sell
End Enum
Partial Public Class TradeCollection
Public Shared Function FromJson(ByVal json As String) As TradeCollection
Return JsonConvert.DeserializeObject(Of TradeCollection)(json, Settings)
End Function
End Class
Public Module Serialize
<Extension()>
Public Function ToJson(ByVal self As TradeCollection) As String
Return JsonConvert.SerializeObject(self, Settings)
End Function
End Module
Friend Module Converter
Public ReadOnly Settings As JsonSerializerSettings = New JsonSerializerSettings With {
.MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
.DateParseHandling = DateParseHandling.None
}
End Module
Friend Class ParseStringConverter
Inherits JsonConverter
Public Overrides Function CanConvert(ByVal t As Type) As Boolean
Return t Is GetType(Long) OrElse t Is GetType(Long?)
End Function
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal t As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
If reader.TokenType = JsonToken.Null Then Return Nothing
Dim value = serializer.Deserialize(Of String)(reader)
Dim l As Long
If Long.TryParse(value, l) Then
Return l
End If
Throw New Exception("Cannot unmarshal type long")
End Function
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal untypedValue As Object, ByVal serializer As JsonSerializer)
If untypedValue Is Nothing Then
serializer.Serialize(writer, Nothing)
Return
End If
Dim value = CLng(untypedValue)
serializer.Serialize(writer, value.ToString())
Return
End Sub
Public Shared ReadOnly Singleton As ParseStringConverter = New ParseStringConverter()
End Class
Friend Class TypeEnumConverter
Inherits JsonConverter
Public Overrides Function CanConvert(ByVal t As Type) As Boolean
Return t Is GetType(TypeEnum) OrElse t Is GetType(TypeEnum?)
End Function
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal t As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
If reader.TokenType = JsonToken.Null Then Return Nothing
Dim value = serializer.Deserialize(Of String)(reader)
Select Case value
Case "buy"
Return TypeEnum.Buy
Case "sell"
Return TypeEnum.Sell
End Select
Throw New Exception("Cannot unmarshal type TypeEnum")
End Function
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal untypedValue As Object, ByVal serializer As JsonSerializer)
If untypedValue Is Nothing Then
serializer.Serialize(writer, Nothing)
Return
End If
Dim value = CType(untypedValue, TypeEnum)
Select Case value
Case TypeEnum.Buy
serializer.Serialize(writer, "buy")
Return
Case TypeEnum.Sell
serializer.Serialize(writer, "sell")
Return
End Select
Throw New Exception("Cannot marshal type TypeEnum")
End Sub
Public Shared ReadOnly Singleton As TypeEnumConverter = New TypeEnumConverter()
End Class
End Namespace
然后安装 Newtonsoft(如果尚未安装)和 Imports
它
然后 Imports HotBit
(或者如果您更改了上面“粘贴此”的命名空间,则导入新的命名空间
然后执行你的请求并查询结果例如:
Sub Main(args As String())
Dim s = New WebClient().DownloadString("https://api.hotbit.io/api/v1/market.deals?market=KIBA/USDT&limit=150&last_id=1521100930&api_key=44812d8f-66d3-01c0-94c3b29305040b03&sign=98EEC3D69D3F70F9BDFED901984B2AA4")
Dim tc = TradeCollection.FromJson(s)
Dim prices = String.Join(","c, tc.Result.Select(Function(r) r.Price))
End Sub
我正在尝试解析 this Json
使用代码:
Dim streamData As Stream = Nothing
Using http As New HttpClient
Dim url As String = "https://api.hotbit.io/api/v1/market.deals?market=KIBA/USDT&limit=150&last_id=1521100930&api_key=44812d8f-66d3-01c0-94c3b29305040b03&sign=98EEC3D69D3F70F9BDFED901984B2AA4"
Dim t As Task(Of Stream) = http.GetStreamAsync(url)
streamData = t.Result
End Using
Dim jsonResponse As JsonNode = JsonNode.Parse(streamData)
Dim result As JsonObject = jsonResponse("result").AsObject
Dim c As String = String.Empty
For Each kvp In result.AsEnumerable
c &= kvp.Value("price").ToString & vbCr
Next
RichTextBox1.Text = c
End Sub
但我在调试时一直收到错误
The node must be of type 'JsonObject'.' on the line
Dim result As JsonObject = jsonResponse("result").AsObject
如果我已经在尝试将其解析为 Jsonobject,怎么会出现错误?
谢谢
试一试..
- 将其粘贴到新的 class 文件中:
Namespace HotBit
Partial Public Class TradeCollection
<JsonProperty("error")>
Public Property [Error] As Object
<JsonProperty("result")>
Public Property Result As List(Of Result)
<JsonProperty("id")>
Public Property Id As Long
End Class
Partial Public Class Result
<JsonProperty("id")>
Public Property Id As Long
<JsonProperty("time")>
Public Property Time As Long
<JsonProperty("price")>
Public Property Price As String
<JsonProperty("amount")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property Amount As Long
<JsonProperty("type")>
Public Property Type As TypeEnum
End Class
Public Enum TypeEnum
Buy
Sell
End Enum
Partial Public Class TradeCollection
Public Shared Function FromJson(ByVal json As String) As TradeCollection
Return JsonConvert.DeserializeObject(Of TradeCollection)(json, Settings)
End Function
End Class
Public Module Serialize
<Extension()>
Public Function ToJson(ByVal self As TradeCollection) As String
Return JsonConvert.SerializeObject(self, Settings)
End Function
End Module
Friend Module Converter
Public ReadOnly Settings As JsonSerializerSettings = New JsonSerializerSettings With {
.MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
.DateParseHandling = DateParseHandling.None
}
End Module
Friend Class ParseStringConverter
Inherits JsonConverter
Public Overrides Function CanConvert(ByVal t As Type) As Boolean
Return t Is GetType(Long) OrElse t Is GetType(Long?)
End Function
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal t As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
If reader.TokenType = JsonToken.Null Then Return Nothing
Dim value = serializer.Deserialize(Of String)(reader)
Dim l As Long
If Long.TryParse(value, l) Then
Return l
End If
Throw New Exception("Cannot unmarshal type long")
End Function
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal untypedValue As Object, ByVal serializer As JsonSerializer)
If untypedValue Is Nothing Then
serializer.Serialize(writer, Nothing)
Return
End If
Dim value = CLng(untypedValue)
serializer.Serialize(writer, value.ToString())
Return
End Sub
Public Shared ReadOnly Singleton As ParseStringConverter = New ParseStringConverter()
End Class
Friend Class TypeEnumConverter
Inherits JsonConverter
Public Overrides Function CanConvert(ByVal t As Type) As Boolean
Return t Is GetType(TypeEnum) OrElse t Is GetType(TypeEnum?)
End Function
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal t As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
If reader.TokenType = JsonToken.Null Then Return Nothing
Dim value = serializer.Deserialize(Of String)(reader)
Select Case value
Case "buy"
Return TypeEnum.Buy
Case "sell"
Return TypeEnum.Sell
End Select
Throw New Exception("Cannot unmarshal type TypeEnum")
End Function
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal untypedValue As Object, ByVal serializer As JsonSerializer)
If untypedValue Is Nothing Then
serializer.Serialize(writer, Nothing)
Return
End If
Dim value = CType(untypedValue, TypeEnum)
Select Case value
Case TypeEnum.Buy
serializer.Serialize(writer, "buy")
Return
Case TypeEnum.Sell
serializer.Serialize(writer, "sell")
Return
End Select
Throw New Exception("Cannot marshal type TypeEnum")
End Sub
Public Shared ReadOnly Singleton As TypeEnumConverter = New TypeEnumConverter()
End Class
End Namespace
然后安装 Newtonsoft(如果尚未安装)和
Imports
它然后
Imports HotBit
(或者如果您更改了上面“粘贴此”的命名空间,则导入新的命名空间然后执行你的请求并查询结果例如:
Sub Main(args As String())
Dim s = New WebClient().DownloadString("https://api.hotbit.io/api/v1/market.deals?market=KIBA/USDT&limit=150&last_id=1521100930&api_key=44812d8f-66d3-01c0-94c3b29305040b03&sign=98EEC3D69D3F70F9BDFED901984B2AA4")
Dim tc = TradeCollection.FromJson(s)
Dim prices = String.Join(","c, tc.Result.Select(Function(r) r.Price))
End Sub