检查你得到的响应是一个格式正确的 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....