-1

I am trying to access my JSON and I get that part done. But when I want access a particular item in the JSON, I get the

    '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance

Here is the following code:

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCredential:credentials];
    [operation setResponseSerializer:[AFJSONResponseSerializer serializer]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
        jsonOutputData = [NSMutableArray arrayWithObject:responseObject];
        NSLog(@"%@", [[jsonOutputData objectAtIndex:0] objectForKey:@"comments"]);

        NSLog(@"The Array: %@",jsonOutputData);

        //[self.newsFeed reloadData];

    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSLog(@"Failure: %@", error);
    }];

    [operation start];

And my JSON is pretty simple:

   [  
    {  
  "id":"2043",
  "user_id":"5",
  "created_time":"2015-03-24 22:32:58",
  "post_type":"3",
  "post_link":"google.ca",
  "message":null,
  "photo":{  
     "height":"266",
     "width":"720",
     "link":"google.ca",
     "thumbnail":"jpg",
     "source":"jpg"
  },
  "likes":"88029",
  "shares":"170",
  "comments":"850",
   }
 ]

Any kind of help would be appreciated! If there are any questions, please ask and I will try my best to give you the necessary information!

Thanks

Here is the JSON output

    (
       (
            {
        comments = 868;
        "created_time" = "2015-03-24 22:32:58";
        id = 2043;
        likes = 88309;
        message = "<null>";
        photo =             {
            height = 266;
            link = "google.ca";
            thumbnail = "jpg";
            width = 720;
        };
        "post_link" = "google.ca";
        "post_type" = 3;
        shares = 175;
        "user_id" = 5;
    }
  )
)
hparmar
  • 53
  • 5
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Mar 27 '15 at 22:10
  • Could you output `jsonOutputData` and paste the result? – Steffen D. Sommer Mar 27 '15 at 22:18
  • ( ( { comments = 868; "created_time" = "2015-03-24 22:32:58"; id = 2043; likes = 88309; message = ""; photo = { height = 266; link = "google.ca"; thumbnail = "jpg"; width = 720; }; "post_link" = "google.ca"; "post_type" = 3; shares = 175; "user_id" = 5; } – hparmar Mar 27 '15 at 22:25
  • Do you know the difference between an NSDIctionary and an NSArray??? – Hot Licks Mar 28 '15 at 02:51

1 Answers1

1

Because you're using jsonOutputData = [NSMutableArray arrayWithObject:responseObject];. So you're putting your responseObject array into a new array (so you have an array in an array). Then you get the first item out of the array, which is responseObject - an array - and try to access it as a dictionary.

Just do

NSLog(@"%@", [[responseObject objectAtIndex:0] objectForKey:@"comments"]);
Wain
  • 118,658
  • 15
  • 128
  • 151
  • How would I use the responseObject in a table view? Since its local instance to the completion block. Could I copy the responseObject to another array using: jsonOutputData = [NSMutableArray arrayWithArray:[responseObject copy]]; And then use that for the table View? – hparmar Mar 27 '15 at 22:31
  • You don't need to copy the array as you create a mutable array with it. Simply `jsonOutputData = [responseObject mutableCopy]` will do. You also don't really need it to be mutable just to source a table view. – Wain Mar 27 '15 at 22:46