1

In the following example I would like to access the ID value from the Flickr JSON (see below):

{
page = 1;
pages = 336567;
perpage = 20;
photo =     (
            {
        farm = 6;
        id = 10573655046;
        isfamily = 0;
        isfriend = 0;
        ispublic = 1;
        owner = "32491986@N08";
        secret = 91349d9115;
        server = 5533;
        title = DSC09356;
    },
            {
        farm = 8;
        id = 10573936633;
        isfamily = 0;
        isfriend = 0;
        ispublic = 1;
        owner = "32491986@N08";
        secret = 6abdcaa69e;
        server = 7295;
        title = DSC09118;
    }

);

So I have tried trying to use the following pattern

 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if(json == nil){
    NSLog(@"NULL");
}else 
    NSLog(@" WORKED");

 NSArray* flickrPics = [json objectForKey:@"photos"];
 NSLog(@"Details: %@", flickrPics); //works
 NSDictionary* lastUpload = [flickrPics objectAtIndex:0];
 //fails on that line but the idea would have been to then
  NSNumber* id = lastUpload[@"photo"][@"id"];

This is the error

2013-10-30 13:43:50.534 JSON1[1508:1303] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8ab3820

So why do I have this error (I have used this pattern on a different JSON object) So how do access the id ?

drlobo
  • 2,139
  • 5
  • 32
  • 42

2 Answers2

0

That is because this line:

 NSArray* flickrPics = [json objectForKey:@"photos"];

Is returning a NSDictionary, not an array

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • In the example I was following (http://www.raywenderlich.com/5492/) NSArray* latestLoans = [json objectForKey:@"loans"]; – drlobo Oct 30 '13 at 14:12
  • Just check what flickrPics is in the console and check it keys – Antonio MG Oct 30 '13 at 14:13
  • Yes it appears to be a NSDictionary so that article I was following must be incorrect. So how do I iterate and access all of the Id's inside that the document ? And how do I iterate through all of the entries in the document ? – drlobo Oct 30 '13 at 15:50
  • To do that, follow this link: http://stackoverflow.com/questions/17960068/with-fast-enumeration-and-an-nsdictionary-iterating-in-the-order-of-the-keys-is and please accept the answer if it worked – Antonio MG Oct 31 '13 at 10:21
0

So I have solved the problem and the problem that needed to be recognised is that in the JSON the photo is a key which holds an array of dictionaries. So how to access specific items inside that array ?

//In this case valueForKey @"photo" will return as an array
NSArray *results = [flickr valueForKey:@"photo"];
//next we can iterate through the array. Each element of the Array is a dictionary
  for(NSDictionary *UserPhotoData in results){
//now we can retrieve the ID (or whatever piece of data you require 
    NSNumber *getId = [UserPhotoData valueForKey:@"id"];
    NSLog(@" id %@", getId);
}

That works.

However I am not sure

1) How a developer knows what is returned by valueForKey. Sometimes it is NSArray, sometimes it is a String or a number.

drlobo
  • 2,139
  • 5
  • 32
  • 42