1

Having some trouble accessing the JSON data in the following URL ( http://jamesstenson.com/portraits/?json=1 ), basically I want to access the "full" "url"'s underneath "attachments". My code at the moment is as follows:

NSError *e = nil;
NSData *jsonFeed = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://jamesstenson.com/portraits/?json=1"]]; 
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:jsonFeed options:NSJSONReadingMutableContainers error: &e];

if (!jsonData) {
    NSLog(@"Error parsing JSON: %@", e);
} else {

    for(NSDictionary *item in [jsonData objectForKey:@"page"]) {
        for(NSDictionary *attachment in [item objectForKey:@"images"]) {
            NSLog(@"%@", attachment);
        }
    }
}

This keeps throwing up an error:

2011-12-21 10:13:39.362 JSON[3463:f803] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a7b500

2011-12-21 10:13:39.363 JSON[3463:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a7b500'

I am aware I am accessing the items wrongly, but cannot figure out how to achieve this. I've tried several solutions such as http://blogs.captechconsulting.com/blog/nathan-jones/getting-started-json-ios5 - but no luck. I am complete newbie to iOS development and have a little knowledge of JSON. Thanks for everyones help in advance.

Nagra
  • 1,529
  • 3
  • 15
  • 25
  • Are you sure that your JSON response (before being parsed) contained the two items exactly named as `page` and `images` ? – Malloc Dec 21 '11 at 10:23
  • You should state exactly where (which line) the error is coming from. It doesn't make sense that you should be getting that specific error with that JSON. But your basic problem is that you never extract the "attachments" array. – Hot Licks Dec 21 '11 at 13:33
  • @Hot Licks The error is in this line for(NSDictionary *item in [jsonData objectForKey:@"page"]) as the error says [__NSCFString objectForKey:]: unrecognized selector , And let me know what you found wrong in my answer – Nakkeeran Dec 21 '11 at 13:46
  • Then what you got back from the JSON parser is an NSString, not an NSDictionary, meaning that it parsed something different from what you think it parsed. (It's always wise to check the object type before you assume it's dictionary or array). – Hot Licks Dec 21 '11 at 16:30

1 Answers1

3

The problem is in you for loop

for(NSDictionary *item in [jsonData objectForKey:@"page"]) 

You won't get NSDictionary in item, it will return you key of Dictionary, which would be NSString

Check this link for each loop in objective c for accessing NSMutable dictionary to know how to traverse through NSDictionay

Below is the modified code for your requirement, might help you

if (!jsonData) {
        NSLog(@"Error parsing JSON: %@", e); } else {
        NSArray *attachments = [[jsonData objectForKey:@"page"] objectForKey:@"attachments"];
        for(NSDictionary *object in attachments) {
              NSLog(@"%@", [object objectForKey:@"images"]);
              NSLog(@"%@", [[[object objectForKey:@"images"] objectForKey:@"full"] objectForKey:@"url"]);
        } 
}
Community
  • 1
  • 1
Nakkeeran
  • 15,296
  • 1
  • 21
  • 22
  • @Hot Licks whats is the reason in down voting my answer,Can you let me know so that i can correct myself – Nakkeeran Dec 21 '11 at 13:38
  • Hey, this is pretty awesome! So close! How do I get access to the Full sized URL then? If you could show me how to spit it out in a NSLog function, I would very happy and mark your answer with a green tick! :D – Nagra Dec 21 '11 at 13:55
  • NSLog(@"%@", [[[object objectForKey:@"images"] objectForKey:@"full"] objectForKey:@"url"]); – Nakkeeran Dec 21 '11 at 14:01
  • "You won't get NSDictionary in item, it will return you key of Dictionary, which would be NSString" – Hot Licks Dec 21 '11 at 16:31