问题
I´m not familiar with JSON data parsing. So far, this is what I´ve come up to through web research: data to parse: https://api.twitch.tv/kraken/streams/
I´m trying to use the JSON.NET/VB.NET framework to do so:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
'(inside a function)
Dim json As JObject = JObject.Parse("https://api.twitch.tv/kraken/streams/")
strResult As String = json.SelectToken("streams").SelectToken("game")
It´s returning me an error message, and I´m sure I don´t have the structure right. How could I make this work? And afterwards, I´d like to loop through the results of the returning array.
Thanks,
回答1:
This should get you going (just a conceptual example - console, not asp.net):
Dim json As JObject = _
JObject.Parse(New WebClient().DownloadString("https://api.twitch.tv/kraken/streams/"))
If json IsNot Nothing AndAlso json.HasValues Then
If json.SelectTokens("streams") IsNot Nothing _
AndAlso json.SelectTokens("streams").Children().Any() Then
Dim games() As JToken = json.SelectTokens("streams").Children().ToArray()
For Each child As JToken In games
Console.WriteLine("game title: {0} game id: {1} for mature audience? {2}", _
child.Item("game"), child.Item("_id"), child.Item("channel").Item("mature"))
Console.WriteLine()
Next
Console.ReadLine()
End If
End If
Hth....
回答2:
This is the code simplified, with the help of EdSF, for the newbies in json (like myself):
Dim apiURL As String = "https://api.twitch.tv/kraken/streams?limit=10&offset=0"
Dim json As JObject = JObject.Parse(New WebClient().DownloadString(apiURL))
If json IsNot Nothing AndAlso json.HasValues Then
If json.SelectTokens("streams") IsNot Nothing AndAlso json.SelectTokens("streams").Children().Any() Then
Dim games() As JToken = json.SelectTokens("streams").Children().ToArray()
For Each child As JToken In games
'Console.WriteLine("game title: {0} game id: {1} for mature audience? {2}", child.Item("game"), child.Item("_id"), child.Item("channel").Item("mature"))
'Console.WriteLine()
lblMensagemSucesso.Text &= "_id=" & child.Item("_id").ToString() & "<br>"
lblMensagemSucesso.Text &= "game=" & child.Item("game").ToString() & "<br>"
lblMensagemSucesso.Text &= "viewers=" & child.Item("viewers").ToString() & "<br>"
lblMensagemSucesso.Text &= "preview=" & child.Item("preview").ToString() & "<br>"
lblMensagemSucesso.Text &= "preview.small=" & child.Item("preview").Item("small").ToString() & "<br>"
lblMensagemSucesso.Text &= "<img src='" & child.Item("preview").Item("small").ToString() & "'/><br>"
lblMensagemSucesso.Text &= "_links.self=" & child.Item("_links").Item("self").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel.status=" & child.Item("channel").Item("status").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel.logo=" & child.Item("channel").Item("logo").ToString() & "<br>"
lblMensagemSucesso.Text &= "<img src='" & child.Item("channel").Item("logo").ToString() & "'/><br>"
lblMensagemSucesso.Text &= "channel.video_banner=" & child.Item("channel").Item("video_banner").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel.url=" & child.Item("channel").Item("url").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel.views=" & child.Item("channel").Item("followers").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel._links.self=" & child.Item("channel").Item("_links").Item("self").ToString() & "<br>"
lblMensagemSucesso.Text &= "channel._links.teams=" & child.Item("channel").Item("_links").Item("teams").ToString() & "<br>"
lblMensagemSucesso.Text &= "<br>"
Next
'Console.ReadLine()
End If
End If
来源:https://stackoverflow.com/questions/24214121/asp-net-json-net-parse-results