user3255746 user3255746 - 1 year ago 81
JSON Question

Accessing friend data from Facebook JSON

I am new to Swift and JSON and could use some quick help. My app has Facebook authentication and will read a list of your friends that have authorized the app as well. Everything logs correctly but I am unfamiliar with how to print the friend Id of each individual friend. Here is my code:

FBSDKGraphRequest(graphPath: "me",
parameters: ["fields": "id, name,friends, first_name, last_name, picture.type(large), email"])
.start(completionHandler: { (connection, result , error) -> Void in
let data:[String:AnyObject] = result as! [String : AnyObject]

And this is the data it logs for friends:

"friends": {
data = (
id = 10104542348623278;
name = "John Smith";

But what I cannot figure out is how to print the id of each individual friend. This is what I have tried unsuccessfully.

for friend in data["friends"] {
let friendId = data["friends"]["id"]

Any help would be very appreciated.

Answer Source
  • friends is a Dictionary.
  • data inside it is an Array of Dictionary.

    if let friendsData = data["friends"] as? [String:AnyObject] {
        if let friends = friendsData["data"] as? [[String:AnyObject]] {
            for friend in friends{
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download