2015-10-18 227 views
1

我試圖獲取Youtube視頻的觀看次數。獲取Youtube視頻viewCount Youtube API v3/Swift

"snippet": { 
    "publishedAt": datetime, 
    "channelId": string, 
    "title": string, 
    "description": string, 
    "thumbnails": { 
     (key): { 
     "url": string, 
     "width": unsigned integer, 
     "height": unsigned integer 
     } 
    }, 
    "channelTitle": string, 
    "tags": [ 
     string 
    ], 
    "categoryId": string, 
    "liveBroadcastContent": string, 
    "defaultAudioLanguage": string 
    }, 

    "statistics": { 
    "viewCount": unsigned long, 
    "likeCount": unsigned long, 
    "dislikeCount": unsigned long, 
    "favoriteCount": unsigned long, 
    "commentCount": unsigned long 

這是需要解析的JSON。我發現了一個在線教程與snippet正確分析,像這樣:

performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -> Void in 
      if HTTPStatusCode == 200 && error == nil { 
       // Convert the JSON data to a dictionary object. 
       let resultsDict = (try! NSJSONSerialization.JSONObjectWithData(data!, options: [])) as! Dictionary<NSObject, AnyObject> 

       // Get all search result items ("items" array). 
       let items: Array<Dictionary<NSObject, AnyObject>> = resultsDict["items"] as! Array<Dictionary<NSObject, AnyObject>> 

       // Loop through all search results and keep just the necessary data. 
       for var i=0; i<items.count; ++i { 
        let snippetDict = items[i]["snippet"] as! Dictionary<NSObject, AnyObject> 
//     let statisticsDict = items[i]["statistics"] as! Dictionary<NSObject, AnyObject> 

        // Create a new dictionary to store the video details. 
        var videoDetailsDict = Dictionary<NSObject, AnyObject>() 
        videoDetailsDict["title"] = snippetDict["title"] 
        videoDetailsDict["channelTitle"] = snippetDict["channelTitle"] 
        videoDetailsDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<NSObject, AnyObject>)["default"] as! Dictionary<NSObject, AnyObject>)["url"] 
        videoDetailsDict["videoID"] = (items[i]["id"] as! Dictionary<NSObject, AnyObject>)["videoId"] 
//     videoDetailsDict["viewCount"] = statisticsDict["viewCount"] 

        // Append the desiredPlaylistItemDataDict dictionary to the videos array. 
        self.videosArray.append(videoDetailsDict) 

你可以在試圖通過做某種類型的此分析顯示了snippet得到的統計數據看到我的努力,但我有沒有運氣,結果對於videoDetailsDict中的「viewCount」總是爲零。

我在做什麼錯?

+0

你應該開始從字典到詞典<字符串,AnyObject> –

+0

改變你的字典聲明確保'targetURL'包含'在部分statistics' –

+0

'var urlString =「https://www.googleapis.com/youtube/v3/search?part=snippet,statistics&q=\(searchBar.text)&type=video&maxResults=15&key=\(apiKey)」'does not work 。你需要單獨做GET嗎? –

回答

0

確保在part參數添加statistics如下: https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.videos.list?part=snippet%252Cstatistics&id=QJYMdGpJQFs&_h=10&

另外,如果你只想要視頻計(或任何具體信息),我建議你使用fields參數做出更小,更明確的迴應。

即讓剛剛viewCountfields=items/statistics(viewCount) https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.videos.list?part=snippet%252Cstatistics&id=QJYMdGpJQFs&fields=items%252Fstatistics(viewCount)&_h=5&