2

I got this JSON:

{
    cover =     {
        id = 1;
};
    description = "Test"
place =     {
        id = 11;
        location =         {
            city = Wheatley;
            };
        name = "Wheatley Provincial Park";
       };
},
{
    cover =     {
        id = 2;
};
    description = "Cool"
place =     {
        id = 22;
        location =         {
            city = Wheatley;
            };
        name = "Wheatley Provincial Park";
       };
}

This is my code:

 if let fbData = result as? [String : Any] {
    print(fbData)

    for events in fbData {
       print (events["name"] as! String)
        //this displays an error
        //Type (Key: String, value: Any) has subscript members
}

}

But I don't know how to loop through them

I already tried these solutions but they never worked:

JSON Parsing in Swift 3

Correctly Parsing JSON in Swift 3

Parsing JSON using Swift 3

Community
  • 1
  • 1
Suhaib
  • 2,031
  • 3
  • 24
  • 36
  • `[String : Any]`: Do you know why you write that? If no, you may want to understand how to read/loop arrays and dictionaries. If yes, JSON are just string, numbers, arrays and dictionaries. – Larme Sep 14 '16 at 07:57
  • It would be helpful if we could see *real* JSON not just that pseudo-JSON you probably got from the console. – Fabio Poloni Sep 14 '16 at 08:15
  • Is this the real JSON? – Bista Sep 14 '16 at 08:19
  • @Mr.UB, some of it. Here is a screenshot of the full http://imgur.com/a/4OQq1. – Suhaib Sep 14 '16 at 08:26
  • So the `result` contain all the content which is shown in the image?? I have updated my answer. – Bista Sep 14 '16 at 08:29

1 Answers1

7
if let array = result as? [String : AnyObject]{
    if let fbData = array["data"] as? [[String : AnyObject]] {
        print(fbData)

        for event in fbData {
            print (event["name"] as! String)
        }
    }
}
  1. result is of Any type
  2. Cast it into Dictionary - [String : AnyObject]
  3. Extract data and cast to Array of Dictionaries - [[String : AnyObject]].
Bista
  • 7,869
  • 3
  • 27
  • 55