-2

I have the following JSON data structure being pulled from FBSDKGraphRequest.

data =     (
            {
        id = "<USER_ID_GOES_HERE>";
        name = "Tom Jones";
        picture =             {
            data =                 {
                "is_silhouette" = 0;
                url = "<USER_IMAGE_URL_GOES_HERE>";
            };
        };
    },
            {
        id = "<USER_ID_GOES_HERE>";
        name = "Tom Jones";
        picture =             {
            data =                 {
                "is_silhouette" = 0;
                url = "<USER_IMAGE_URL_GOES_HERE>";
            };
        };
    },
            {
        id = "<USER_ID_GOES_HERE>";
        name = "Tom Jones";
        picture =             {
            data =                 {
                "is_silhouette" = 0;
                url = "<USER_IMAGE_URL_GOES_HERE>";
            };
        };
    },

I want to extract the data and place it into an array. I am having difficulty with the first "data" key.

Heres my FBSDKGR:

let params = ["fields": "name, picture.type(large)"]
    let request = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params)
    request!.start { (connection, result, error) -> Void in

        if error != nil {
            print("There is some error getting friends", error!)
        }
        else if result != nil {
            print("Here is the result", result!)
David Henry
  • 1,972
  • 20
  • 43
  • essentially I want to get this data into a UITableView – David Henry Mar 05 '17 at 17:50
  • 5
    Did you search on SO? There are hundreds of related questions to parse JSON. Read the JSON! `[]` are arrays (to be subscripted by index) `{}` are dictionaries (to be subscripted by key). However the listing is a console output of a Swift collection type. There `()` are arrays and `{}` are dictionaries. – vadian Mar 05 '17 at 17:56

2 Answers2

1

With an extra dependency, if you don't mind.

import SwiftyJSON

let json = JSON(result) // after you get result from FBSDKGR
let data = json["data"].array
for (index, _) in data.enumerated() {
     if let id = json["data", index, "id"].int {
          // add to your array
          if let name = json["data", index, "name"].string {
              // continue nesting...
         }
     } 
}
Faris Sbahi
  • 646
  • 7
  • 15
0

As your JSOND dta is array of dictionary so , we can cast the result by the following way . First of all we will take two array , one is for names list and other is for picture details . here picture details array is array of dictionary . let's go for code

var namesArray = [String]()
var ImageDetailsArrayDict= [[String :AnyObject]]()

let params = ["fields": "name, picture.type(large)"]
    let request = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params)
    request!.start { (connection, result, error) -> Void in

        if error != nil {
            print("There is some error getting friends", error!)
        }
        else if result != nil {
            print("Here is the result", result!)
         if let response = result["data"] as? [[String :AnyObject]] {

               for i in 0..<response.count {
                  namesArray.append(response[i]["name"] as! String)                
                  ImageDetailsArrayDict.append(response[i]["picture"])

                 }
              }
          }
      }

Now , we have two array . so we can easily populate it in the tableView . Good luck .

Harish Singh
  • 765
  • 1
  • 12
  • 23
  • there is still an error type any has no subscript members on: if let response = result["data"] as? [[String :AnyObject]] { – David Henry Mar 05 '17 at 20:22