我正在尝试将我的 JSON 解析为 NSDictionary。这是我用于解析的方法。
+ (NSDictionary *)executeGenkFetch:(NSString *)query
{
query = [NSString stringWithFormat:@"%@", query];
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"[%@ %@] sent %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), query);
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
return results;
}
这就是我如何使用这个功能。
+(NSArray *)getCommentsWithParam:(NSString *)Param
{
NSString *request = [NSString stringWithFormat:@"https://graph.facebook.com/comments/?ids=%@",Param];
NSLog(@"request is %@",request);
NSString *vfk = [NSString stringWithFormat:@"%@.comments.data",Param];
return [[self executeGenkFetch:request] valueForKey:vfk];
}
问题是当我记录从 getCommentsWithParam 返回的 NSArray 时,我总是得到 NULL。但是当我查看日志时
NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
我得到了我想要的 JSON。对出了什么问题有任何帮助吗?