Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 解析youtube数据v3 api_Ios_Json_Swift_Xcode - Fatal编程技术网

Ios 解析youtube数据v3 api

Ios 解析youtube数据v3 api,ios,json,swift,xcode,Ios,Json,Swift,Xcode,我想解析v3的youtube api数据的响应 我得到的JSON响应是: { "kind": "youtube#playlistItemListResponse", "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/l1epVamXLkcsfpr63PWaASmovg4\"", "nextPageToken": "CAMQAA", "pageInfo": { "totalResults": 27, "resultsPerPage": 3 }, "ite

我想解析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"
  }
}
]
}
我想访问所有的数据。。例如,如何在代码段中获取标题?和默认的url缩略图。。等等

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

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

定义与您获取的数据相匹配的结构(这是一个很好的方法),然后使用
JSONDecoder
解析数据:

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

例如,如何打印值?这取决于如何命名结构的属性。如果您不使用JSON中的名称,这可能很简单,例如,
print(\(youtubeData.items[0].id)”)