我刚刚开始使用JSON。. NET和我有一些麻烦从JSON项目获取值。
在过去的几天里,我遇到了一些代码,这些代码允许我获取名称,但不能获取相关值。
我正在与抽搐工作。tv Web API。下面是我的代码:
Dim sUrl As String = Convert.ToString("https://api.twitch.tv/kraken/streams/") & sUsername
Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(sUrl), HttpWebRequest)
wRequest.ContentType = "application/json"
wRequest.Accept = "application/vnd.twitchtv.v3+json"
wRequest.Method = "GET"
Dim wResponse As WebResponse = wRequest.GetResponse()
Dim dataStream As Stream = wResponse.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim res As String = reader.ReadToEnd()
Dim outer As JToken = JToken.Parse(res)
Dim inner As JObject = outer("stream").Value(Of JObject)()
Dim keys As List(Of String) = inner.Properties().[Select](Function(p) p.Name).ToList()
For Each k As String In keys
Debug.WriteLine(k)
Next
reader.Close()
wResponse.Close()
原始JSON如下:
{
"_links": {
"self": "https: //api.twitch.tv/kraken/streams/jojosaysbreee",
"channel": "https: //api.twitch.tv/kraken/channels/jojosaysbreee"
},
"stream": {
"_id": 16717827552,
"game": "TomClancy'sRainbowSix: Siege",
"viewers": 15,
"created_at": "2015-09-30T21: 19: 10Z",
"video_height": 720,
"average_fps": 59.9630366205,
"is_playlist": false,
"_links": {
"self": "https: //api.twitch.tv/kraken/streams/jojosaysbreee"
},
"preview": {
"small": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-80x45.jpg",
"medium": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-320x180.jpg",
"large": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-640x360.jpg",
"template": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-{width}x{height}jpg"
},
"channel": {
"_links": {
"self": "http: //api.twitch.tv/kraken/channels/jojosaysbreee",
"follows": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/follows",
"commercial": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/commercial",
"stream_key": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/stream_key",
"chat": "http: //api.twitch.tv/kraken/chat/jojosaysbreee",
"features": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/features",
"subscriptions": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/subscriptions",
"editors": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/editors",
"videos": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/videos",
"teams": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/teams"
},
"background": null,
"banner": null,
"broadcaster_language": "en",
"display_name": "JOJOsaysbreee",
"game": "TomClancy'sRainbowSix: Siege",
"logo": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-profile_image-26a326e1c867f257-300x300.jpeg",
"mature": true,
"status": "BetaHype<3",
"partner": false,
"url": "http: //www.twitch.tv/jojosaysbreee",
"video_banner": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-channel_offline_image-67b08d519585b45f-640x360.jpeg",
"_id": 41382559,
"name": "jojosaysbreee",
"created_at": "2013-03-16T09: 33: 34Z",
"updated_at": "2015-10-01T05: 15: 26Z",
"delay": null,
"followers": 2318,
"profile_banner": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-profile_banner-6abce6a882f4f9e4-480.jpeg",
"profile_banner_background_color": "#ffffff",
"views": 15939,
"language": "en"
}
}
}
代码的响应是"stream"中的所有名称:
_id
game
viewers
created_at
video_height
average_fps
is_playlist
_links
preview
channel
我想要完成的是在每一个项目之后获得所有相关的值,但我似乎无法得到它的权利。我知道它需要迭代更深,但我已经尝试过IEnumerable方法,也没有成功。
将所有属性名选择到一个列表中,如下所示:
Dim keys As List(Of String) = inner.Properties().[Select](Function(p) p.Name).ToList()
通过这种方式,您将无法从keys
获得除属性名称之外的任何内容。相反,循环遍历Properties()
集合并检查每个JProperty
:
For Each prop As JProperty In inner.Properties()
Debug.WriteLine("{0} - {1}", prop.Name, prop.Value)
Next
另外,我建议做一些改变。您用来发出请求的代码比它应该的更难:)
Dim sUrl As String = Convert.ToString("https://api.twitch.tv/kraken/streams/") & sUsername
不需要调用Convert.ToString("")
-它已经是一个字符串。而且(虽然这是一个意见问题),我认为匈牙利符号的变量是没有必要的。这就简单多了:
Dim url As String = "https://api.twitch.tv/kraken/streams/" & username
使用WebClient
:
Dim client As New WebClient()
client.Headers.Add(HttpRequestHeader.Accept, "application/vnd.twitchtv.v3+json")
Dim responseJson = client.DownloadString(url)
完整的示例:
Dim username As String = "???"
Dim url As String = "https://api.twitch.tv/kraken/streams/" & username
Dim client As New WebClient()
client.Headers.Add(HttpRequestHeader.Accept, "application/vnd.twitchtv.v3+json")
Dim responseJson As String = client.DownloadString(url)
Dim outer As JToken = JToken.Parse(responseJson)
Dim inner As JObject = outer("stream").Value(Of JObject)
For Each prop As JProperty In inner.Properties()
Console.WriteLine($"{prop.Name} - {prop.Value}")
Next
容易peasy !:)