解析youtube数据v3 api

时间:2018-07-17 12:21:49

标签: ios json swift xcode

我想解析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"
  }
}
]
}

,我想访问所有数据..例如,如何在摘要中获取标题?以及默认的网址缩略图..等等..

 if let videos = receivedResponse["items"] as? [[String: Any]] {
    for video in videos{
        print(video)
    }
 }

但是我不知道如何获取数组中的字典..如何进一步解析它并获取json中的每个元素?

1 个答案:

答案 0 :(得分:0)

定义与要获取的数据匹配的结构(https://app.quicktype.io是快速启动此结构的好方法),然后使用JSONDecoder来解析数据:

struct YoutubeV3 { ... }
let youtubeData = try JSONDecoder().decode(YoutubeV3.self, from: data)