AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这是推荐方式 https://github.com/AFNetworking/AFNetworking#get-request发送GET
AFNetworking 2.0 中的请求。我想获取json中特定键的值,所以我想使用responseObject
as NSDictionary
。这就是我正在尝试的:
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:kNilOptions error:&jsonError];
它不起作用:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary bytes]: unrecognized selector sent to instance 0xa048120'
我怎样才能获得特定键的值responseObject
?
默认情况下,AFHTTPRequestOperationManager
sets responseSerializer
to an AFJSONResponseSerializer
实例,所以responseObject
已经是你解析的 JSON (在你的情况下,它将是一个NSDictionary
按照你说的)。
然后,就像使用字典一样使用它:
NSString *value = responseObject[@"someKey"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)