1

How to Parse Json in vb.net; want to create bittrex ticker.

Request I made with following code:

  Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader

    Try

        request = DirectCast(WebRequest.Create("https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-ltc"), HttpWebRequest)
        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())


        rawresp = reader.ReadToEnd()


    Catch ex As Exception
        Console.WriteLine(ex.ToString)
        MsgBox(ex.ToString)
    Finally
        If Not response Is Nothing Then response.Close()
    End Try

And i got following json response:

{"success":true,"message":"","result":[{"MarketName":"BTC-LTC","High":0.01670094,"Low":0.01610000,"Volume":47495.02156742,"Last":0.01628948,"BaseVolume":777.22088098,"TimeStamp":"2018-01-21T13:18:23","Bid":0.01624001,"Ask":0.01628948,"OpenBuyOrders":2146,"OpenSellOrders":8104,"PrevDay":0.01622000,"Created":"2014-02-13T00:00:00"}]}

Want value of Last to be shown in textbox, so i tried to parse it with "Newtonsoft" as follows:

  Dim jsonArray As JArray = JArray.Parse(rawresp)
    For Each item As JObject In jsonArray
        textboxLast.Text = item.SelectToken("Last").ToString

    Next

But getting error :(

2
  • {} is object and [] is array, so probably something like JObject.Parse(rawresp)!result(0)!Last Commented Jan 21, 2018 at 14:00
  • You may want to specify the error you're getting Commented Jan 21, 2018 at 14:09

1 Answer 1

3
Dim json As String = rawresp
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim jsonArray As JArray = jsonObject("result")

For Each item As JObject In jsonArray
    textboxLast.Text = item.SelectToken("Last").ToString
Next

It has to do with the format of the JSON. it is not an array, but an object that contains an array, so you have to first parse the object, and then take the array out of it to parse properly. You missed that one extra step, which I added in above code snippet

Sign up to request clarification or add additional context in comments.

2 Comments

and if it was { "success": true, "message": "", "result": { "Currency": "BTC", "Balance": 0.00000020, "Available": 0.00000020, "Pending": 0.00000000, "CryptoAddress": "xxxxxxxxxxx" } } then how to parse its balance @leo-muller
just the first two lines, and then this: textboxLast.Text = jsonObject("result")("Currency")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.