Hi, until yesterday I was parsing correctly a JSON API. Today instead I'm getting an exception thrown while debugging ERROR in Asks and ERROR in Bids . I'm not really understanding why. The JSOn doesn't seems to be changed.
The API uri is order.depth
and this is the code I'm using:
Public Shared Function ToKMB(ByVal num As Double) As String
If num > 999999999 OrElse num < -999999999 Then
Return num.ToString("0,,,.###B", CultureInfo.InvariantCulture)
ElseIf num > 999999 OrElse num < -999999 Then
Return num.ToString("0,,.##M", CultureInfo.InvariantCulture)
ElseIf num > 999 OrElse num < -999 Then
Return num.ToString("0,.#K", CultureInfo.InvariantCulture)
Else
Return num.ToString(CultureInfo.InvariantCulture)
End If
End Function
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
Sub ReCalcASKS()
Label19.Text = ToKMB(Calc(currASKS, GetDouble(TextBox7.Text), GetDouble(TextBox8.Text)))
End Sub
Sub ReCalcBIDS()
Label18.Text = ToKMB(Calc(currBIDS, GetDouble(TextBox10.Text), GetDouble(TextBox9.Text)))
End Sub
Function Calc(lst As String()(), min As Double, max As Double) As Double
If lst Is Nothing Then Return 0
Dim sum As Double
For Each s As String() In lst
Dim a As Double = GetDouble(s(0))
Dim b As Double = GetDouble(s(1))
If a >= min AndAlso a <= max Then sum += b
Next
Return sum
End Function
Private URI As String = "https://api.hotbit.io/api/v1/order.depth?market=KIBA/USDT&limit=150&interval=1e-8"
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
Private Sub OutputAsks(asks As String()())
'asks
Try
Dim contents As New StringBuilder
Dim sum As Integer = 0
currASKS = asks
ReCalcASKS()
For Each ask() As String In asks
Dim i As Integer = GetInteger(ask(1))
sum += i
contents.AppendLine(String.Join(" - ", {ask(0), ToKMB(GetDouble(ask(1)))}))
ToKMB(i)
Next
Label23.Text = ToKMB(sum)
RichTextBox1.BeginInvoke(Sub()
RichTextBox1.Text = contents.ToString()
RichTextBox1.Lines = RichTextBox1.Lines.
Where(Function(x) Not String.IsNullOrEmpty(x)).
OrderByDescending(Function(x) Decimal.Parse(x.Split(" "c)(0))).ToArray
ScrollToBottom(RichTextBox1)
End Sub)
Catch ex As Exception
OrderBook.Stop()
MessageBox.Show("ERROR in Asks")
End Try
End Sub
Private Sub OutputBids(bids As String()())
'bids
Try
Dim contents As New StringBuilder
Dim sum As Integer = 0
currBIDS = bids
ReCalcBIDS()
For Each Bid() As String In bids
Dim i As Integer = GetInteger(Bid(1))
sum += i
contents.AppendLine(String.Join(" - ", {Bid(0), ToKMB(GetDouble(Bid(1)))}))
ToKMB(i)
Next
Label24.Text = ToKMB(sum)
RichTextBox2.BeginInvoke(Sub()
RichTextBox2.Text = contents.ToString()
ScrollToTop(RichTextBox2)
End Sub)
Catch ex As Exception
OrderBook.Stop()
MessageBox.Show("ERROR in Bids")
End Try
End Sub
Function GetDouble(s As String) As Double
Dim v As Double = 0.0D
If Double.TryParse(s, v) Then Return v
Return 0.0D
End Function
Function GetInteger(s As String) As Integer
Dim v As Integer = 0
If Integer.TryParse(s, v) Then Return v
Return 0
End Function
and I'm calling it with:
Dim root As Root = Await GetJsonFromApi()
OutputAsks(root.result.asks)
OutputBids(root.result.bids)
I didn't changed anything in the code so the only reason could be a JSON changed but from a first view I don't see any changing. . . This code was working perfectly thanks also to LesHay that helped me a lot on this code.
Thanks