Home > OS >  Deserialize JSON to a module
Deserialize JSON to a module

Time:02-18

I need to parse a JSON that comes from a crypto exchange API. In this case, I need to parse my open orders. If there are no open orders, the Json is :

{"error":null,"result":{"KIBAUSDT":{"limit":100,"offset":0,"total":0,"records":null}},"id":299639536}

which seems a dictionary. As soon as I create 1 order, the Json become like this:

{"error":null,"result":{"KIBAUSDT":{"limit":100,"offset":0,"total":1,"records":[{"user":2996434,"id":82651361103,"market":"KIBAUSDT","freeze":"0.001","fee_stock":"","source":"web","type":1,"side":2,"ctime":1645022087.403207,"mtime":1645022087.403207,"price":"0.00001","amount":"100","taker_fee":"0.0035","maker_fee":"-0.003","left":"100","deal_stock":"0","deal_money":"0","deal_fee":"0","alt_fee":"0","deal_fee_alt":"0","status":0}]}},"id":305636871}

which seems more an array (Please correct me if I'm wrong). Those lines are beign created for each single order I'm creating. So, on enter image description here

it is kind of weird how I'm getting the first error "FromJson is not a member of kibausdt", since total property is inside the Kibausdt class. This issue is solved by using Dim total = Example.FromJson((Await s)) but I'm not really sure that's the answer.

Thanks

CodePudding user response:

Public Async Function ExecuteAsync() As Task
    Dim wc As New WebClient
    'you can await this task and get the json string result instead of adding it to a list first
    Dim json = Await wc.DownloadStringTaskAsync("https://api.hotbit.io/api/v1/order.pending?market=KIBA/USDT&offset=0&limit=100&api_key=44812d8f-66d3-01c0-94c3b29305040b03&sign=F3330B924E1873B9C8FAB40A25D7B851")

    'deserialize the json
    Dim rootObject = Example.FromJson(json)
    'navigate to Kibausdt
    Dim kibausdt = rootObject.result.KIBAUSDT

    'check total
    If kibausdt.total = 0 Then
        RichTextBox1.Text = "0 opened orders"
    Else
        'loop through records
        For Each record In kibausdt.records
            Dim side As String = record.side.ToString()
            Dim amount As Long = record.amount
            Dim price As String = record.price
            RichTextBox1.Text &= side & amount & price
        Next
    End If
End Function
  • Related