-1

I use sbjson to parse json, this is the Json I'm trying to parse :

{"R":"0","S":"","T":"","M":[{"C00":"2013-08-16 17:35:03"}]}

and this is my code:

NSString *response = [self post:@"9903" content:payload state:@""];
NSDictionary *dict = [response JSONValue];
NSString *result = [dict objectForKey:@"R"];
NSLog(@"result=%@",result);
if ([@"0" isEqualToString:result]) {
    NSDictionary *msg = [dict objectForKey:@"M"];
    NSString *C00 = [msg objectForKey:@"C00"];//here the exception Statement
    NSString *tokenString = [NSString stringWithFormat:@"%@",C00];
    NSLog(@"tokenString%@",tokenString);
    return tokenString;
}else {
    return nil;
}

the exception log:

2013-08-16 17:45:44.902 VEP[4731:c07] -[__NSArrayM objectForKey:]: unrecognized selector    sent to instance 0x7250300
2013-08-16 17:45:44.903 VEP[4731:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7250300'

what's wrong? thanks for your help!

lgw150
  • 170
  • 1
  • 3
  • 13
  • 3
    Read the error message and compare it with your JSON data. (Spoiler: `[dict objectForKey:@"M"]` returns an *array*, not a *dictionary*.) – Martin R Aug 16 '13 at 09:54
  • 1
    Btw.: "JSON __NSArrayM objectForKey unrecognized selector" has *many* Google hits which describe (and explain) exactly your problem. – Martin R Aug 16 '13 at 10:00

1 Answers1

3

Because your key 'M:' contain array of dictionary not only dictionary. So, write it as a,

NSDictionary *msg = [[dict objectForKey:@"M"] objectAtIndex:0];
iPatel
  • 46,010
  • 16
  • 115
  • 137