我试图使用RestKit来调查Giant Bomb的API,但我很难找到我正在寻找的答案。
来自API的JSON响应看起来像这样(我已编辑它以减少返回的结果,因为它返回很多):
{
"error": "OK",
"limit": 100,
"offset": 0,
"number_of_page_results": 100,
"number_of_total_results": 45810,
"status_code": 1,
"results": [
{
"name": "Desert Strike: Return to the Gulf"
},
{
"name": "Breakfree"
},
{
"name": "Hyperballoid Deluxe: Survival Pack"
}
],
"version": "1.0"
}
我试图映射'结果'以我的名义命名'属性。
在这里'我的' (从复制/粘贴段中积累)代码:
NSURL *baseUrl = [NSURL URLWithString:@"http://www.giantbomb.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseUrl];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[GameDBGame class]];
[objectMapping addAttributeMappingsFromDictionary:@{
@"name": @"name"
}];
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:objectMapping
pathPattern: @"/api/search/"
keyPath: @"results"
statusCodes:[NSIndexSet indexSetWithIndex: 200]];
[objectManager addResponseDescriptor:responseDescriptor];
NSDictionary *params = @{
@"api_key":@"[[ I Put my API key here ]]",
@"format":@"json",
@"query":@"Game name",
@"resources":@"games",
@"field_list":@"name"
};
[objectManager getObjectsAtPath: @"api/search/"
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"This was a success");
_gameDBGames = [NSMutableArray arrayWithArray:mappingResult.array];
NSLog(@"%@" , _gameDBGames);
for ( int x = 0 ; x < [_gameDBGames count] ; x++ )
{
GameDBGame *item = [_gameDBGames objectAtIndex:x];
NSLog( @"Game Name %@" , item.name );
}
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"There was an error: %@" , error);
}];
使用此代码,RestKit将空数组映射到我的_gameDBGames数组,而不是映射结果列表。
注意:我使用的是RestKit v 0.20.0
感谢您的任何建议!