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);
}];
这是在AFNetworking 2.0中发送
GET
请求的recommended way。我想获取json中特定键的值,所以我想使用responseObject
作为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
将responseSerializer
设置为AFJSONResponseSerializer
实例,因此responseObject
已经是您解析的JSON(在您的情况下,根据您的说法将是NSDictionary
)。
然后,就像使用字典一样使用它:
NSString *value = responseObject[@"someKey"];