ios - 获取 Youtube 视频 viewCount Youtube API v3/Swift

标签 ios json swift youtube

我正在尝试获取 Youtube 视频的观看次数。

"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”的结果始终为零.

我做错了什么?

最佳答案

确保在部分参数中添加statistics,如下: https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.videos.list?part=snippet%252Cstatistics&id=QJYMdGpJQFs&_h=10&

另外,如果您只需要视频计数(或任何特定信息),我建议您使用 fields 参数来做出更小更清晰的响应。

即仅使用 fields=items/statistics(viewCount) 获取 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&

关于ios - 获取 Youtube 视频 viewCount Youtube API v3/Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33202259/

相关文章:

python - 类型错误 : unbound method decode() must be called with JSONDecoder instance as first argument (got PrintJson instance instead)

ios - JSON 文件从服务器下载,文件加载了 8 次?

ios - 将 AVAudioPCMBuffer 写入压缩的 AVAudioFile

ios - 在继承中传递变量 (Obj - C)

mysql - 检查包含 json 字符串的列是否具有特定值

macos - 将完成处理程序添加到 presentViewControllerAsSheet(NSViewController)?

ios - 如何更改自定义 UICollectionView 布局引擎中的默认布局动画曲线?

arrays - 在 Swift 中按降序创建数组的最短方法

ios - 使用 Google Sign In for iOS 时未捕获 NSInvalidArgumentException

ios - 在 AVPlayer 的请求中添加自定义 header 字段