使用“Newtonsoft”Json.net 解析 VB.NET 中的 Json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48367318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse Json in VB.NET with "Newtonsoft" Json.net
提问by Amit kumar
How to Parse Json in vb.net; want to create bittrex ticker.
如何在 vb.net 中解析 Json;想要创建 bittrex 股票代码。
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:
我得到了以下 json 响应:
{"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:
希望 Last 的值显示在文本框中,所以我尝试用“Newtonsoft”解析它,如下所示:
Dim jsonArray As JArray = JArray.Parse(rawresp)
For Each item As JObject In jsonArray
textboxLast.Text = item.SelectToken("Last").ToString
Next
But getting error :(
但得到错误:(
采纳答案by Leo Muller
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
它与 JSON 的格式有关。它不是数组,而是一个包含数组的对象,所以必须先解析对象,然后从中取出数组才能正确解析。你错过了我在上面的代码片段中添加的额外步骤

