解析 youtube 数据 v3 api
Posted
技术标签:
【中文标题】解析 youtube 数据 v3 api【英文标题】:parse youtube data v3 api 【发布时间】:2018-07-17 12:21:49 【问题描述】:我想解析 v3 的 youtube api 数据的响应
我得到的 JSON 响应是:
"kind": "youtube#playlistItemListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/l1epVamXLkcsfpr63PWaASmovg4\"",
"nextPageToken": "CAMQAA",
"pageInfo":
"totalResults": 27,
"resultsPerPage": 3
,
"items": [
"kind": "youtube#playlistItem",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/KyN5GfWqc2WFc37j5GcuhThVXig\"",
"id": "UExTbVNFYzhaNmxpcTgyT3RlcHBSbS1rb3l0ZWdhUFgyQy41NkI0NEY2RDEwNTU3Q0M2",
"snippet":
"publishedAt": "2017-10-03T17:59:36.000Z",
"channelId": "UCuv25YFiKY3NUnuG7q7F3ng",
"title": "أغرب الفرص الضائعة (الجولة 5) من الدوري السعودي",
"description": "تابع مباريات #دوري_جميل مباشرة و حصريا على موقع وتطبيق #دوري_بلس : \n\nhttp://www.dawriplus.com\n\nتويتر : https://twitter.com/dawriplusksa?s=09\n\nانستجرام :http://instagram.com/dawriplusksa\n\nفيس بوك : https://m.facebook.com/DawriPlusKSA/",
"thumbnails":
"default":
"url": "https://i.ytimg.com/vi/Tdt06VCCko0/default.jpg",
"width": 120,
"height": 90
,
"medium":
"url": "https://i.ytimg.com/vi/Tdt06VCCko0/mqdefault.jpg",
"width": 320,
"height": 180
,
"high":
"url": "https://i.ytimg.com/vi/Tdt06VCCko0/hqdefault.jpg",
"width": 480,
"height": 360
,
"standard":
"url": "https://i.ytimg.com/vi/Tdt06VCCko0/sddefault.jpg",
"width": 640,
"height": 480
,
"maxres":
"url": "https://i.ytimg.com/vi/Tdt06VCCko0/maxresdefault.jpg",
"width": 1280,
"height": 720
,
"channelTitle": "دوري بلس",
"playlistId": "PLSmSEc8Z6liq82OteppRm-koytegaPX2C",
"position": 0,
"resourceId":
"kind": "youtube#video",
"videoId": "Tdt06VCCko0"
,
"contentDetails":
"videoId": "Tdt06VCCko0",
"videoPublishedAt": "2017-10-03T13:32:19.000Z"
,
"kind": "youtube#playlistItem",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/QHbLatdcFE-i6Pxt5mku-QFX29c\"",
"id": "UExTbVNFYzhaNmxpcTgyT3RlcHBSbS1rb3l0ZWdhUFgyQy4yODlGNEE0NkRGMEEzMEQy",
"snippet":
"publishedAt": "2018-05-07T17:24:35.000Z",
"channelId": "UCuv25YFiKY3NUnuG7q7F3ng",
"title": "أغرب الفرص الضائعة (الجولة 26) من الدوري السعودي",
"description": "أغرب الفرص الضائعة (الجولة 26) من الدوري السعودي\n\nتابع جميع مباريات الدوري السعودي للمحترفين فقط وحصرياً عبر تطبيق دوري بلس\nhttp://www.dawriplus.com\n\nتويتر : https://twitter.com/dawriplusksa\nانستجرام : http://instagram.com/dawriplusksa\nفيسبوك : https://m.facebook.com/DawriPlusKSA/",
"thumbnails":
"default":
"url": "https://i.ytimg.com/vi/yegbCi2DcE4/default.jpg",
"width": 120,
"height": 90
,
"medium":
"url": "https://i.ytimg.com/vi/yegbCi2DcE4/mqdefault.jpg",
"width": 320,
"height": 180
,
"high":
"url": "https://i.ytimg.com/vi/yegbCi2DcE4/hqdefault.jpg",
"width": 480,
"height": 360
,
"standard":
"url": "https://i.ytimg.com/vi/yegbCi2DcE4/sddefault.jpg",
"width": 640,
"height": 480
,
"maxres":
"url": "https://i.ytimg.com/vi/yegbCi2DcE4/maxresdefault.jpg",
"width": 1280,
"height": 720
,
"channelTitle": "دوري بلس",
"playlistId": "PLSmSEc8Z6liq82OteppRm-koytegaPX2C",
"position": 1,
"resourceId":
"kind": "youtube#video",
"videoId": "yegbCi2DcE4"
,
"contentDetails":
"videoId": "yegbCi2DcE4",
"videoPublishedAt": "2018-04-17T10:34:50.000Z"
,
"kind": "youtube#playlistItem",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/3PqM5VVO1MDpiMSOpiLQw3JCoLU\"",
"id": "UExTbVNFYzhaNmxpcTgyT3RlcHBSbS1rb3l0ZWdhUFgyQy4wMTcyMDhGQUE4NTIzM0Y5",
"snippet":
"publishedAt": "2018-05-07T17:26:13.000Z",
"channelId": "UCuv25YFiKY3NUnuG7q7F3ng",
"title": "أغرب الفرص الضائعة (الجولة 25) من الدوري السعودي",
"description": "أغرب الفرص الضائعة (الجولة 25) من الدوري السعودي\n\nتابع جميع مباريات الدوري السعودي للمحترفين فقط وحصرياً عبر تطبيق دوري بلس\nhttp://www.dawriplus.com\n\nتويتر : https://twitter.com/dawriplusksa\nانستجرام : http://instagram.com/dawriplusksa\nفيسبوك : https://m.facebook.com/DawriPlusKSA/",
"thumbnails":
"default":
"url": "https://i.ytimg.com/vi/KUzswfTBDBI/default.jpg",
"width": 120,
"height": 90
,
"medium":
"url": "https://i.ytimg.com/vi/KUzswfTBDBI/mqdefault.jpg",
"width": 320,
"height": 180
,
"high":
"url": "https://i.ytimg.com/vi/KUzswfTBDBI/hqdefault.jpg",
"width": 480,
"height": 360
,
"standard":
"url": "https://i.ytimg.com/vi/KUzswfTBDBI/sddefault.jpg",
"width": 640,
"height": 480
,
"maxres":
"url": "https://i.ytimg.com/vi/KUzswfTBDBI/maxresdefault.jpg",
"width": 1280,
"height": 720
,
"channelTitle": "دوري بلس",
"playlistId": "PLSmSEc8Z6liq82OteppRm-koytegaPX2C",
"position": 2,
"resourceId":
"kind": "youtube#video",
"videoId": "KUzswfTBDBI"
,
"contentDetails":
"videoId": "KUzswfTBDBI",
"videoPublishedAt": "2018-04-12T12:13:21.000Z"
]
我想访问所有数据..例如如何在 sn-p 中获取标题?以及默认的 url 缩略图 .. 等等 ..
if let videos = receivedResponse["items"] as? [[String: Any]]
for video in videos
print(video)
但我不知道如何获取数组中的字典..如何进一步解析并获取 json 中的每个元素?
【问题讨论】:
Correctly Parsing JSON in Swift 3的可能重复 【参考方案1】:定义与您获得的数据相匹配的结构(https://app.quicktype.io 是一个很好的启动方法),然后使用JSONDecoder
解析数据:
struct YoutubeV3 ...
let youtubeData = try JSONDecoder().decode(YoutubeV3.self, from: data)
【讨论】:
那么我如何打印值? 这取决于您如何命名结构的属性。如果您保留 JSON 中的名称,这可能很简单,例如print("\(youtubeData.items[0].id)")
以上是关于解析 youtube 数据 v3 api的主要内容,如果未能解决你的问题,请参考以下文章
使用新的 YouTube API v3 解析 YouTube 订阅者数量
YouTube 数据 API V3 是不是适用于 YouTube 音乐?
喜欢使用 YouTube 数据 API v3 在 YouTube 上使用访问令牌的视频吗?