0

I'm trying to output some data using NSJSONSerializationusing the code below.

This log statement works NSLog(@"publicData: %@", publicData); but then it crashes when I try to get the "description" of the dictionary NSLog(@"data: %@", [dict objectForKey:@"description"]);

my error message: unrecognized selector sent to instance. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance...'

I've included the NSLog output.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:@"http://******.com/api/feed"]];

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSError *jsonParsingError = nil;
NSArray *publicData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

NSLog(@"data from publicData: %@", publicData);
NSDictionary *dict;

for(int i=0; i<[publicData count];i++)
{
    dict= [publicData objectAtIndex:i];
    NSLog(@"data: %@", [dict objectForKey:@"description"]);

}


publicData: {
data =     (
            {
        address =             {
     address = "street address";
            city = cityname;
            lat = "42.000237034667";
            lng = "12.492805660226";
            province = provincename;
        };
        "category_id" = 20;
        description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
        id = 1;
        slug = "title-in-en";
        title = "title in En";
    },
            {
        address =             {
            address = "street address";
            city = cityname;
            lat = "42.000237034667";
            lng = "12.492805660226";
            province = provincename;
        };
        "category_id" = 20;
        description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
        id = 5;
        slug = "title-in-en";
        title = "title in En";
    },

thanks for any help

2
  • In your case, publicData is an NSDictionary with a single object for key "data", not an array. Access an NSDictionary with objectForKey, not objectAtIndex. Commented May 29, 2013 at 19:10
  • @MarcusAdams I thought I was doing that though. Where "dict" is the dictionary and publicData is the nsarray. Is that incorrect? thanks! NSDictionary *dict; for(int i=0; i<[publicData count];i++) { dict= [publicData objectAtIndex:i]; NSLog(@"data: %@", [dict objectForKey:@"description"]); } Commented May 29, 2013 at 19:22

2 Answers 2

1

I optimized a little the code provided by codeplasma and it's looking like this:

 NSDictionary *publicData =  [NSJSONSerialization JSONObjectWithData:response
                                                                options:0              
                                                                  error:&jsonParsingError];
    NSArray *objects = [publicData objectForKey:@"data"];
    for(NSDictionary *element in objects) {
       NSLog(@"the description is %@",[element objectForKey:@"description"]);
    } 

Your publicData is NSDictionary not NSArray.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks but I'm getting an NSArray has no method 'objectForKey' error.
@hanumanDev can you post the full JOSN structure without your NSLog, maybe from your borwser :)
I just edited my question above and added the output from the browser. thanks for the help!
that worked perfectly! I was using an array instead of a dictionary. thanks again!
0

I don't know if this will work, but you should try to replace your for loop with this:

for(int i=0; i<[[publicData objectForKey:@"data"] count];i++)
{
    dict= [[publicData objectForKey:@"data"] objectAtIndex:i];
    NSLog(@"data: %@", [dict objectForKey:@"description"]);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.