asp.net解析json并返回数组,get item不是数组?

时间:2017-12-11 20:30:22

标签: asp.net arrays

如何从通过crysonia api检索到的json返回Label,AskPrice和LastPrice,其中json响应中有许多记录。我需要循环它们;但要做到这一点,我需要在数组中。我收到错误“从JsonReader读取JArray时出错。当前JsonReader项不是数组:StartObject。路径'',第1行,第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

1 个答案:

答案 0 :(得分:2)

对于强类型,最好创建 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

然后执行:

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