我正在嘗試決議這個 API
這基本上是一個帶有“詢價”和“出價”的訂單簿。我如何決議他們將要價與出價分開?例如 api 以 asks 屬性開頭所以如果 Json 是 {"asks":[["0.00001555","3264400"],["0.00001556","3662200"],["0.00001573","3264400"]
我期待這樣的輸出:
[問]
價格-數量
0.00001555 - 3264400
0.00001556 - 3662200
0.00001573 - 3264400
電子抄送
之后有"bids":[["0.00001325","300"],["0.00001313","100"],["0.00001312","1051400"],["0.00001311","1300000"],["0.0000131","9336700"]
所以我期待
[投標]
價格-數量
0.00001325- 300
0.00001313 - 100
0.00001312 - 1051400
0.00001311 - 1300000
0.0000131 - 9336700
電子抄送
我知道如何用代碼決議每個值:
Dim streamData As Stream = Nothing
Using http As HttpClient = New HttpClient
Dim url As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=100&interval=1e-8"
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
For Each kvp In result.AsEnumerable
c &= kvp.Value("key?!?!?!?").ToString & ", "
Next
但在這種情況下,沒有我可以“決議”的鍵,而且值的格式為 ["Price", "Quantity"] 而且我不知道如何在詢價和出價之間拆分結果,可能會拆分它們在兩個不同的richtextboxes多行..任何幫助將不勝感激謝謝
uj5u.com熱心網友回復:
使用將為您創建類的JsonUtils,然后您可以將它們復制到您的專案中。
在這種情況下,它將創建:
Public Class Result
Public Property asks As String()()
Public Property bids As String()()
Public Property seqNum As Integer
Public Property prevSeqNum As Integer
Public Property lastTs As Double
End Class
Public Class Root
Public Property [error] As Object
Public Property result As Result
Public Property id As Integer
End Class
請注意,將其
error作為屬性名稱會引發編譯器錯誤,因此您需要將其包含在方括號中,例如[error].
您還可以大大簡化您的代碼:
Private Async Sub MyMethod()
Dim root As Root = Await GetJsonFromApi()
For each ask In root.result.asks
Console.WriteLine(ask(0))
Next
End Sub
Private Async Function GetJsonFromApi() As Task(Of Root)
Using http As New HttpClient()
Dim url As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=100&interval=1e-8"
Return Await http.GetFromJsonAsync(Of Root)(url)
End Using
End Function
或者,如果您愿意并且想要處理例外,例如:
Private Async Sub MyMethod()
Dim root As Root = Await GetJsonFromApi()
For each ask In root.result.asks
Next
End Sub
Private Async Function GetJsonFromApi() As Task(Of Root)
Using http As New HttpClient()
Dim url As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=100&interval=1e-8"
Dim response As HttpResponseMessage = Await http.GetAsync(url)
If response.IsSuccessStatusCode
Return Await response.Content.ReadFromJsonAsync(Of Root)
End If
End Using
Return New Result
End Function
作為旁注,我正在等待您使用關鍵字Await看到的回應。我更喜歡這樣做而不是.Result最后打電話。
最后,我會考慮將url變數設為常量:
Private Const URI As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=100&interval=1e-8"
...
Dim response As HttpResponseMessage = Await http.GetAsync(URI)
還有Newtonsoft.Json可用于將 JSON 反序列化為類:
Private Async Function GetJsonFromApi() As Task(Of Root)
Using http As New HttpClient(),
response As HttpResponseMessage = await http.GetAsync(URI)
Return JsonConvert.DeserializeObject(Of Root)(Await response.Content.ReadAsStringAsync())
End Using
End Function
Note you will have to add this as a Nuget package to your application. Also I've assumed the URI is now a constant and I've nested the
Usingstatements.
To output these you will have to use String.Join and to ensure the UI doesn't freeze you can call Control.BeginInvoke:
Private Sub OutputAsks(asks As String()())
Dim contents As New StringBuilder
contents.AppendLine("[asks]")
contents.AppendLine("Price - Quantity")
For Each ask In asks
contents.AppendLine(String.Join(" - ", ask))
Next
RichTextBox1.BeginInvoke(Sub()
RichTextBox1.Text = contents.ToString()
End Sub)
End Sub
Private Sub OutputBids(bids As String()())
Dim contents As New StringBuilder
contents.AppendLine("[bids]")
contents.AppendLine("Price - Quantity")
For Each bid In bids
contents.AppendLine(String.Join(" - ", bid))
Next
RichTextBox2.BeginInvoke(Sub()
RichTextBox2.Text = contents.ToString()
End Sub)
End Sub
Note, as part of the
Control.BeginInvokeyou don't have to strictly callControl.EndInvokeas noted in the docs:You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke will block until the return value can be retrieved.
The full solution - taking into account your Button, Timer, and RichTextbox controls - would look something like :
Private Const URI As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=10&interval=1e-8"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
UpdateContents()
End Sub
Private Async Sub UpdateContents()
Dim jsonContents As String = Await GetJsonFromApi()
Dim root As Root = DeserialiseJsonToRoot(jsonContents)
OutputAsks(root.result.asks)
OutputBids(root.result.bids)
End Sub
Private Async Function GetJsonFromApi() As Task(Of String)
Using http As New HttpClient(),
response As HttpResponseMessage = Await http.GetAsync(URI)
Return Await response.Content.ReadAsStringAsync()
End Using
End Function
Private Function DeserialiseJsonToRoot(json As String) As Root
Return JsonConvert.DeserializeObject(Of Root)(json)
End Function
Private Sub OutputAsks(asks As String()())
Dim contents As New StringBuilder
contents.AppendLine("[asks]")
contents.AppendLine("Price - Quantity")
For Each ask In asks
contents.AppendLine(String.Join(" - ", ask))
Next
RichTextBox1.BeginInvoke(Sub()
RichTextBox1.Text = contents.ToString()
End Sub)
End Sub
Private Sub OutputBids(bids As String()())
Dim contents As New StringBuilder
contents.AppendLine("[bids]")
contents.AppendLine("Price - Quantity")
For Each bid In bids
contents.AppendLine(String.Join(" - ", bid))
Next
RichTextBox2.BeginInvoke(Sub()
RichTextBox2.Text = contents.ToString()
End Sub)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426204.html
上一篇:當我清除搜索框時查詢仍保留在表中
