0

I'm trying to parse some JSON to a NSArray but I'm getting the following error:

[__NSCFDictionary length]: unrecognized selector sent to instance 0x6d7a160

The area of code that's throwing this error is:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError* error;
NSLog(responseString);
NSArray *jsonArray = [NSJSONSerialization 
          JSONObjectWithData:responseData
          options:NSJSONReadingMutableContainers
          error:&error];
parties2=jsonArray;
NSLog([parties2 objectAtIndex:0]); //Exception thrown

[tableView reloadData];

}

parties2 is previously defined as:

parties2=[NSArray arrayWithObjects:nil];

My ResponseString looks like

[{"Name":"party 1.1","GreekName":"FoA 1","GreekID":325,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":1000,"price":15.0},{"Name":"party 1.2","GreekName":"FoA 1","GreekID":325,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":300,"price":20.0},{"Name":"party 1.3","GreekName":"FoA 1","GreekID":325,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":5000,"price":25.0},{"Name":"party 2.1","GreekName":"FoA 2","GreekID":326,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":500,"price":25.0},{"Name":"party 2.2","GreekName":"FoA 2","GreekID":326,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":300,"price":30.0},{"Name":"party 3.1","GreekName":"FoA 3","GreekID":327,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":0,"price":50.0},{"Name":"party 5.1","GreekName":"FoA 5 ","GreekID":329,"schoolName":"FoA University","schoolID":10,"numberAttending":0,"maxNumberAttending":300,"price":15.75}]

This is my first venture with ObjC, I'm coming from a .Net C# background so chances are I'm missing a very simple thing.

Thanks :)

Matthew Arkin
  • 4,460
  • 2
  • 27
  • 28
  • It's apparently returning an ‘NSMutableDictionary*‘ in this case though I don't see why. Is ‘error‘ updated? You might try immediately writing the data out again (also with the 'NSJSONSerialization' class) to see how it was interpreted. To sanity-check the result you could use an ‘NSObject‘ method, e.g. ‘if ([jsonArray isKindOfClass:[NSArray class]]) ...‘. – Kevin Grant Jul 28 '12 at 06:19
  • So error is Nil, if I print the description of jsonArray while debugging (apparently I can't NSLog an array?) it says looks like an array) and it did enter the if statement so it looks likes its a NSArray. Its still giving me [_NSCFDictionart length]: unrecongnized selector sent to instance – Matthew Arkin Jul 28 '12 at 15:39

1 Answers1

0

Your JSON parsing is correct. The error is being thrown because of a bad syntax in your NSLog() statement.

NSLog() takes an NSString as an argument, but your are trying to pass an NSDictionary instead (i.e. the first element of your NSArray), which is causing the error.

The solution to this is to use an NSString format string to convert the NSDictionary object to a string, like this:

NSLog(@"%@",[parties2 objectAtIndex:0]);
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Thanks, yea I was doing that to debug something that was happening in another area of code which turned out to be an issue of me pulling code when parties2 was nil. – Matthew Arkin Jul 28 '12 at 17:25