1

I have this API response from FB Graph Request and am having trouble accessing beyond the "data" layer to get to name, description, start_time, id, etc.

 ({
events =     {
    data =         (
                    {
            description = "This is the description";
            "end_time" = "2020-06-14T01:00:00-0400";
            id = 234258508603994942;
            name = "Event 1";
            place =                 {
                id = 102184499823699;
                location =                     {
                    city = Montreal;
                    country = Canada;
                    latitude = "45.5038";
                    longitude = "-73.5744";
                    state = QC;
                };
                name = "Montreal, Quebec";
            };
            "rsvp_status" = attending;
            "start_time" = "2020-06-11T22:00:00-0400";
        },
                    {
            description = "Doors open at 6pm All methods of delivery will be delayed;
            id = 2456759751111373;
            name = "John Oliver";
            place =                 {
                id = 203030439809517;
                location =                     {
                    city = "San Francisco";
                    country = "United States";
                    latitude = "37.79118";
                    longitude = "-122.41293";
                    state = CA;
                    street = "1111 California St";
                    zip = 94108;
                };
                name = "The Masonic";
            };
            "rsvp_status" = unsure;
            "start_time" = "2020-01-02T19:00:00-0800";
        }
    );
    paging =         {
        cursors =             {
            after = QVFIUnlNRzFiTmwtQk9BRFVmN09OUkxHNWkxzQXNSOGJsWURQb21IdFFITHVXdU5xUC1jTTd5Tml5YVNiS3kyejNWYUk5czJ5dmtn;
            before = QVFIUnE3TVA2S21VVVNNbXlDWGZA2ZAFZAUTXU5h0NGRBS0EtUHB6QWpJNURMU1Q5dTRQV2RhVWJlYS01U3RtdUh0RGcxaDlzOTIzNzF2eTln;
        };
    };
};
id = 2624126090194735;
})

Here's the code that isn't able to get to the "data" objects"

if let dictionary = result as? [String: Any] {
    if let dictionary1 = dictionary["events"] as? [String: Any]{
        if let nestedDictionary = dictionary1["data"] as? [String: Any]{
           let eventDescription = nestedDictionary["description"] as? String
           let fbEventId = nestedDictionary["id"] as? String
           let eventName = nestedDictionary["name"] as? String
           let eventStart = nestedDictionary["start_time"] as? String
        }
    }
}

As I'm sure is obvious, this wont get into the "data" dictionary which is stopping my ability to get to the event details such as description, id, name, etc. How do I get access into "Data"?

8
  • let nestedDictionary = dictionary1["data"] as? [String: Any] should be let nestedDictionary = dictionary1["data"] as? [[String: Any]] Commented Oct 15, 2019 at 5:14
  • Hi i think in your JSON response, keys are missing double commas"" like id = 2624126090194735; should be "id" = 2624126090194735; Please check Commented Oct 15, 2019 at 5:16
  • @RajeshKumarR when I do that I get the following error: Cannot subscript a value of type '[[String : Any]]' with an argument of type 'String'. Commented Oct 15, 2019 at 5:17
  • @dakrawat I checked, quotes ("") only on some of the keys, not on all, not sure why but that's the way I get the dictionary back from fb Commented Oct 15, 2019 at 5:19
  • i Think ,If u can use codable protocol for json parsing then that is much easy to do with optional chaining. Commented Oct 15, 2019 at 5:21

1 Answer 1

2

As mentioned in the comment by @RajeshKumar, data is an array of dictionary. Then you can loop through that array to get each dictionary and related values as,

if let dictionary = result as? [String: Any] {
    if let dictionary1 = dictionary["events"] as? [String: Any]{
        if let dataList = dictionary1["data"] as? [[String: Any]] {
           dataList.forEach { dictionary in 
               let eventDescription = dictionary["description"] as? String
               let fbEventId = dictionary["id"] as? String
               let eventName = dictionary["name"] as? String
               let eventStart = dictionary["start_time"] as? String
               print(eventDescription)
               print(fbEventId)
               print(eventName)
               print(eventStart)
           }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats it! Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.