【发布时间】:2014-01-01 18:54:31
【问题描述】:
我正在尝试读取以下 json:
[{"result":"1","msg":"Login Successful.”,”title":"Login","redirect":"index.php","servers":"{\"140\":\"10 minute Email\"}","server”:”xxx.xxx.xxx.xxx”}]
像这样:
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"result"] integerValue];
NSLog(@"%d",success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Logged in Successfully." :@"Login Success!"];
} else {
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Login Failed!"];
}
但我收到以下错误:
2014-01-01 20:44:08.857 服务器监视器[9704:70b] -[__NSArrayM objectForKey:]: 无法识别的选择器发送到实例 0x8e59950
2014-01-01 20:44:08.857 服务器监视器[9704:70b] 例外: -[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0x8e59950
我认为问题在于 json 是一个数组,我该如何处理呢?
【问题讨论】:
-
你为什么使用 sbjson?这是相当过时的做法。 developer.apple.com/library/ios/documentation/Foundation/…
-
这是我通过谷歌搜索发现的
-
是的,Apple 在 ios5 中添加了上述类。此外,如果您使用 CoreData,您可以使用一个框架来无缝进出实体,使用 JSON 作为 NSCoding 的格式(内置序列化协议)。
-
错误信息明确指出你认为的dictionary,其实是array。
标签: ios objective-c json sbjson