5

I'm making the following simple request to Facebook. My goal is to grab the names of all a user's photo albums:

[[AppDelegate sharedDelegate].facebook requestWithGraphPath:@"me/albums" andDelegate:[AppDelegate sharedDelegate]];

Then, I get a JSON response from the server like such:

data =     (
            {
        "can_upload" = 1;
        count = 4;
        "cover_photo" = 321491374598618;
        "created_time" = "2012-06-04T21:46:23+0000";
        from =             {
            id = 100002132746588;
            name = "Owner....";
        };
        id = 321491371265285;
        link = "http://www.facebook.com/album.php?fbid=321491371265285&id=100002132746588&aid=73680";
        name = "Photos";
        privacy = custom;
        type = normal;
        "updated_time" = "2012-06-04T22:08:39+0000";
    },
            {
        "can_upload" = 0;
        count = 1;
        "cover_photo" = 318401854907570;
        "created_time" = "2012-05-31T00:00:35+0000";
        description = "Which Friend Stalks You the Most?";
        from =             {
            id = 100002132746588;
            name = "Owner....";
        };
        id = 318401848240904;
        link = "http://www.facebook.com/album.php?fbid=318401848240904&id=100002132746588&aid=73163";
        name = "Which Friend Stalks You the Most?";
        privacy = friends;
        type = normal;
        "updated_time" = "2012-05-31T00:00:36+0000";
    },

And so on. Problem is, when I try to parse the data with:

NSLog(@"%@", [result objectForKey:@"name"]);

I get null. I assume this is happening because the NSDictionary that the data is returned in does not know which name entry to concentrate on. How do I parse the data so that I get the names of all the albums?

Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
The Kraken
  • 3,158
  • 5
  • 30
  • 67
  • Read my answer may be get your solution.....http://stackoverflow.com/questions/30207465/ios-facebook-album-photos-picker/31789234#31789234 – Maulik shah Sep 26 '15 at 14:14

1 Answers1

3

"result" is actually an array of dictionaries, so loop through the array and then grab name from each element in the array.

for (NSDictionary *anAlbum in [result objectForKey:@"data"]) {
    NSLog(@"%@", [anAlbum objectForKey:@"name"]);
}
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • Thanks for your response. I tried your above code, which makes total sense, but got the following error: `[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x898d320`. It looks like when parsed, each dictionary is actually a `NSCFString`. – The Kraken Jun 05 '12 at 02:45
  • Ok, please run this code and post the output: for (NSDictionary *anAlbum in result) { NSLog(@"%@", anAlbum); } – Andy Obusek Jun 05 '12 at 03:11
  • Here's the output: `data`, and then on a new line `paging`. `data` is an entry at the top of the JSON output, and `paging` is at the bottom. – The Kraken Jun 05 '12 at 20:55
  • Can you post the full output of that for loop with nslog in your question? Your comment is not clear. Thx – Andy Obusek Jun 05 '12 at 22:35
  • `2012-06-05 17:46:42.626 App[19857:707] data 2012-06-05 17:46:42.627 App[19857:707] paging` – The Kraken Jun 05 '12 at 22:47
  • That's it. If you look at the raw JSON text, you can see that "data" is contained at the very top of the response, with all of the actual information I requested contained within it. – The Kraken Jun 05 '12 at 22:48
  • I suppose I could just try using SBJSON to parse it. – The Kraken Jun 05 '12 at 22:48
  • Ahh ok, I updated my answer, try out the new for loop I posted in my answer, the key difference is that you need to extract the "data" object as a dictionary, and paging is a string (thus the exception). – Andy Obusek Jun 06 '12 at 00:29
  • 1
    Perfect, thanks for your help--I really appreciate it. In case you have any spare time to examine another FB-related question, I have one posted here at: http://stackoverflow.com/questions/10905615/ios-facebook-sdk-keeps-giving-same-request-url. Thanks again! – The Kraken Jun 06 '12 at 23:22