Error while parsing Json API

Mattia Fanti 356 Reputation points
2022-03-01T11:38:58.41+00:00

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

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2022-03-01T15:22:05.283+00:00

    Hi

    OK, now run again and when the exception happens, hover the mouse pointer over the 'Sum' in the error line and examine the value given, and over the 'Ask(1)' and/or Bids(1) in the line above. Note the values.

    For some reason, a simple Integer seems to be exceeding the maximum Integer value allowed? I suspect that the Asks(1) or Bids(1) are providing either very large numbers. (Not happening here with the code I am using - must be the same incoming data that you are getting)

    Do not clutter the screen with the Watch window, just show the exception pop-up.


1 additional answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-03-01T14:37:10.74+00:00

    Hi
    Maybe your Timer Interval is too small causing the code to be interupted mid Loop and overflowing variables in some way.

    'Do you mean using my code and that url you are getting the data?' - I have an updated version of the code I last posted - the one using DataGridViews. It gets the data using the same method, and your problems are NOT down to the incoming data.

    You should comment out the Try...Catch block and let the exception occur at the problematic place - then post the exception message here.