Search code examples
swiftalamofireswifty-json

SwiftyJSON YouTube API


I am using SwiftyJSON to retrieve data from the youtube api. I am writing swift 4 code.

I'm trying to print out the description

Here is what I have:

func getFeedVideos() {
    Alamofire.request(API_URL, method: .get, parameters: ["part":"snippet", "playlistId":PLAYLIST_ID,"key":API_KEY], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in

        if let value = response.result.value {
            let json = JSON(value)

            print(json["items"]["snippet"]["description"].stringValue)
        }

    }
}

But no description is being printed out.

Below is the youtube API:

  "items" : [
    {
      "kind": "youtube#playlistItem",
      "etag": etag,
      "id": string,
      "snippet": {
        "publishedAt": datetime,
        "channelId": string,
        "title": string,
        "description": string,
        "thumbnails": {
          (key): {
            "url": string,
            "width": unsigned integer,
            "height": unsigned integer
          }
        },
        "channelTitle": string,
        "playlistId": string,
        "position": unsigned integer,
        "resourceId": {
          "kind": string,
          "videoId": string,
        }
      },
    }

Solution

  • If you look closely at the first line of the response, you'll notice the following:

    "items" : [

    The [ indicates items is an array, which means you should try

    items[0]["snippet"]["description"].stringValue