-3

i want to parse the response of youtube api data for v3

the JSON response i got is:

{
 "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"
  }
}
]
}

and i want to access all of the data .. for example how to get title inside the snippet? and the default url thumbnails .. and so on ..

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

but i don't know how to get the dictionaries inside the array .. how to parse it further and get every and each element in the json?

lamatat
  • 503
  • 1
  • 10
  • 21
  • 1
    Possible duplicate of [Correctly Parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Prashant Tukadiya Jul 17 '18 at 12:30

1 Answers1

0

Define structs that match the data you're getting (https://app.quicktype.io is a great way to jumpstart this), and then use JSONDecoder to parse the data:

struct YoutubeV3 { ... }
let youtubeData = try JSONDecoder().decode(YoutubeV3.self, from: data)
Gereon
  • 17,258
  • 4
  • 42
  • 73
  • then how can i for example print values? – lamatat Jul 17 '18 at 13:16
  • That depends on how you name your struct's properties. If you keep the names from the JSON, this could be as simple as e.g. `print("\(youtubeData.items[0].id)")` – Gereon Jul 17 '18 at 13:20