0
你好,我想解析一个JSON,但每次我尝试我失败。我已经尝试过最近使用过的这种方法,并且在除了这个之外的所有其他项目中工作。 json是有效的。解析NSCFString objectForKeyedSubscript错误
NSString *post = [NSString stringWithFormat:@"x=2&y=3&z=1"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[[NSURL alloc] initWithString:@"link-to-php.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSError *error;
NSMutableDictionary *getJsonData = [NSJSONSerialization
JSONObjectWithData:requestHandler
options:NSJSONReadingMutableContainers
error:&error];
if(error)
{
NSLog(@"%@", [error localizedDescription]);
}
else {
NSArray *json = getJsonData[@"data"];
for (NSDictionary *jsn in json)
{
NSLog(@"dea %@",jsn[@"content"]);
}
}
我的JSON的代码是这样
{"status":1,"error_message":null,"data":{"name":"Test","img":"","content":"Continut de test"}}
我得到的错误是在“数据”键
[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x17e7b7c0
2015-08-03 14:58:50.706 InfoCons[4418:1720073] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x17e7b7c0'
正是在这行你得到的错误? –