检查你得到的回应是正确格式的JSON文件,如果是检查其类:
NSError *jsonError = nil;
NSJSONSerialization *myJSONObject = [NSJSONSerialization JSONObjectWithData:theNSDataObained options:NSJSONReadingMutableContainers error:&jsonError];
if(jsonError != nil){
NSLog(@"JSON Error: %@", jsonError);
return;
}
//An NSJSONSerialization object will either be an NSDictionary or
//an NSArray, figure out what it is:
NSLog(@"JSON class is: %@", [myJSONObject class]);
//if it's a NSDictionary:
if([myJSONObject isKindOfClass:[NSDictionary class]]){
NSDictionary *myJSONDictionary = (NSDictionary *) myJSONObject;
for(id key in myJSONDictionary){
id value = [myJSONDictionary valueForKey:key];
[value doStuff];
}
}
else //if it's a NSArray....
什么是您返回的数据是什么样子?如果你将它解释为一个字符串(使用'[[[[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding] autorelease]'),它看起来像JSON吗? – AliSoftware
使用dataWithContentsofURL创建的变量数据确实会返回一些十六进制值,但是当我使用上面评论中给出的命令时,它将返回Null。任何线索在这里出了什么问题? – user1534235
使用的编码完全错误。您收到的数据可能不是UTF8编码,正如我所假设的那样,但是使用其他编码方式(您必须与Web服务或服务文档的开发人员了解它使用的编码) – AliSoftware