【问题标题】:exception 'NSInvalidArgumentException':-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x752c670 [duplicate]异常“NSInvalidArgumentException”:-[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0x752c670 [重复]
【发布时间】:2013-08-16 09:49:07
【问题描述】:

我使用sbjson来解析json, 这是我要解析的 Json:

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

这是我的代码:

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;
}

异常日志:

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'

怎么了?感谢您的帮助!

【问题讨论】:

  • 阅读错误消息并将其与您的 JSON 数据进行比较。 (剧透:[dict objectForKey:@"M"] 返回一个 array,而不是 dictionary。)
  • 顺便说一句:“JSON __NSArrayM objectForKey unrecognized selector”有 许多 Google 点击,它们准确地描述(并解释)了您的问题。

标签: iphone ios sbjson


【解决方案1】:

因为您的键 'M:' 不仅包含字典,还包含字典数组。所以,写成,

NSDictionary *msg = [[dict objectForKey:@"M"] objectAtIndex:0];

【讨论】:

  • 它成功了!但我还是很困惑,你能给我详细的吗?谢谢!
  • 你有什么问题??
  • 嗯,我看看json的定义,现在明白了,谢谢iPatel!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2012-04-12
  • 2013-12-26
  • 2012-07-16
相关资源
最近更新 更多