0

How do I return the Label, AskPrice and LastPrice from this json retrieved via the crytopia api of which there are many records in the json response. I need to loop through them; but to do that I need it in an array. I get error "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1."

 'json example   {"Success":true,"Message":null,"Data":[{"TradePairId":1261,"Label":"$$$/BTC","AskPrice":0.00000012,"BidPrice":0.00000010,"Low":0.00000010,"High":0.00000012,"Volume":68064.22361439,"LastPrice":0.00000011,"BuyVolume":13524665.12308717,"SellVolume":19130552.28589448,"Change":10.0,"Open":0.00000010,"Close":0.00000011,"BaseVolume":0.00734778,"BuyBaseVolume":0.31169133,"SellBaseVolume":2961236.99999879}],"Error":null}

        Dim url as string = "https://www.cryptopia.co.nz/api/GetMarkets"
        Dim theurl As New Uri(url)
        Using webClient = New System.Net.WebClient()
            Dim json = webClient.DownloadString(theurl)
            Dim d As JArray = JArray.Parse(json)
        End Using
3
  • It's JObject, it's not an array, {} are not arrays. And your JSON example (I now noticed the url) does not finish with a }, which means it's invalid. Commented Dec 11, 2017 at 20:38
  • Updated json example, it's valid now, that is just an example, if you hit that url you see the whole thing. Not sure if you have an answer? Can you provide one I can try? Commented Dec 11, 2017 at 20:42
  • Yeah I'll type it now as an answer Commented Dec 11, 2017 at 20:47

1 Answer 1

2

For strong typing it's best to create a ViewModel

Class MarketWrapper
    Property Success As String
    Property Message As String
    Property Data As IEnumerable(Of DataWrapper)
End Class

Class DataWrapper
    Property TradePairId As Int32
    Property Label As String
    Property AskPrice As Double
    Property BidPrice As Double
    Property Low As Double
    Property High As Double
    Property Volume As Double
    Property LastPrice As Double
    Property BuyVolume As Double
    Property SellVolume As Double
    Property Change As Double
    Property Open As Double
    Property Close As Double
    Property BaseVolume As Double
    Property BuyBaseVolume As Double
    Property SellBaseVolume As Double
End Class

Then execute as:

Dim url as string = "https://www.cryptopia.co.nz/api/GetMarkets"
Dim theurl As New Uri(url)
Using webClient = New System.Net.WebClient()
    Dim json = webClient.DownloadString(theurl)
    Dim dataWrapper = JsonConvert.DeserializeObject(Of MarketWrapper)(json)
End Using
Sign up to request clarification or add additional context in comments.

Comments

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.