2015-10-01 133 views
1

我剛剛開始使用JSON.NET,並且從JSON項獲取值時遇到了一些問題。從twitch.tv獲取值JSON響應

我在過去幾天遇到過一些代碼,這些代碼可以讓我獲取名字,但不是他關聯的值。

我正在使用twitch.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" 
     } 
    } 
} 

從代碼的響應是所有的名字從 「流」:

_id 
game 
viewers 
created_at 
video_height 
average_fps 
is_playlist 
_links 
preview 
channel 

我是什麼試圖完成每個項目後獲得所有相關的值,但我似乎無法做到。我知道它需要迭代更深,但我已經嘗試過IEnumerable方法,並且在那裏也不成功。

任何和所有的幫助,非常感謝。

回答

1

你選擇所有的屬性名與該行的列表:

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! :)

+0

AH!找到了!非常感謝您的快速回復。我仍然試圖熟悉JSON.NET API。我想我現在明白如何使用它。我在不同的地方嘗試了我的其他代碼中的prop.Value,但無法得到它。再次,它像一個魅力。非常感謝。 – LuckoftheLefty

+0

@LuckoftheLefty沒問題!樂於幫助。 :)順便說一句,增加了一些額外的建議。整個代碼片段可以大大簡化。 –

+0

我接受你的建議改變了它。我誠實地獲得了我從查看C#示例中獲得的代碼,並轉換了我所知道的最好的代碼。我剛剛開始編程大約9個月前,並且一直在做所有基於Web的設計(php,ajax,jquery等),並且沒有「官方」培訓。無論如何,再次感謝所有的幫助。 – LuckoftheLefty