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

5
  • [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. Commented Sep 14, 2016 at 7:57
  • It would be helpful if we could see real JSON not just that pseudo-JSON you probably got from the console. Commented Sep 14, 2016 at 8:15
  • Is this the real JSON? Commented Sep 14, 2016 at 8:19
  • @Mr.UB, some of it. Here is a screenshot of the full imgur.com/a/4OQq1. Commented Sep 14, 2016 at 8:26
  • So the result contain all the content which is shown in the image?? I have updated my answer. Commented Sep 14, 2016 at 8:29

1 Answer 1

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]].
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.