我正在使用JSON框架编写iPhone本机应用程序.
我的应用程序正在使用JSON访问Web服务.我们发送的JSON数据有嵌套对象,下面是提供的数据示例:
{
"model": {
"JSONRESPONSE": {
"authenticationFlag": true,
"sessionId": "3C4AA754D77BFBE33E0D66EBE306B8CA",
"statusMessage": "Successful Login.",
"locId": 1,
"userName": "Joe Schmoe"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用objectForKey和valueForKey NSDictionary方法解析时遇到问题.我不断收到invalidArgumentException运行时错误.
例如,我想查询"authenticationFlag"元素的响应数据.
谢谢,迈克西雅图
以下是Objective-C代码,我试图在两个NSString值之间进行比较,但是它会引发运行时错误.这是代码:
NSDictionary *innerContent=[JSONResponseDict valueForKey:@"JSONRESPONSE"];
NSString *authFlag = [innerContent valueForKey:@"authenticationFlag"];
NSLog(@"authFlag = %@",authFlag);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message: [NSString stringWithFormat:@"authenticationFlag = %@",authFlag]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
// This block is problematic
if ( [authFlag isEqualToString:@"1"]){
NSLog(@"Logged in");
self.view = homeView;
} else {
NSLog(@"Not logged in");
}
Run Code Online (Sandbox Code Playgroud)
请注意,NSString authFlag已经过测试,确实具有有效的字符串值.authFlag的值为"1"或"0"(它来自使用json-framework对JSON调用的响应).
这是运行时错误:
[Session started at 2009-03-29 19:21:00 -0700.]
2009-03-29 19:21:11.186 taggle[4144:20b] userEmail=user@domain.com&password=opensesame
2009-03-29 19:21:11.653 taggle[4144:20b] authFlag = 1
2009-03-29 19:21:11.655 taggle[4144:20b] *** -[NSCFBoolean isEqualToString:]: unrecognized selector sent to …Run Code Online (Sandbox Code Playgroud)