这是我的代码:
NSError *error = nil;
SBJsonParser *parserJson = [[SBJsonParser alloc] init];
NSDictionary *jsonObject = [parserJson objectWithString:webServiceResponse error:&error];
[parserJson release], parserJson = nil;
//Test to see if response is different from nil, if is so the parsing is ok
if(jsonObject != nil){
//Get user object
NSDictionary *userJson = [jsonObject objectForKey:@"LoginPOST2Result"];
if(userJson != nil){
self.utente = [[User alloc] init];
self.utente.userId = [userJson objectForKey:@"ID"];
}
而 Json 字符串 webServiceResponse 是:
{"LoginPOST2Result":
"{\"ID\":1,
\"Username\":\"Pippo\",
\"Password\":\"Pippo\",
\"Cognome\":\"Cognome1\",
\"Nome\":\"Nome1\",
\"Telefono\":\"012345678\",
\"Email\":null,
\"BackOffice\":true,
\"BordoMacchina\":false,
\"Annullato\":false,
\"Badge\":1234}"
}
执行此行时出现问题:
self.utente.userId = (NSInteger *) [userJson objectForKey:@"ID"];
错误是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x6861520'
该错误似乎是由于对象 userJson 不是 NSDictionary 而是 NSCFString 类型,因此不响应消息 objectForKey:。
我在哪里做错了?