我會盡力解釋我是如何想的映射完成:RestKit 2級對象的映射
TOP LVL JSON對象包含玩家對象,它是Player
對象 陣列,每個Player
對象包含數組,我想數組中的每個對象都是Event
對象(自定義對象)。
,因爲現在我有Player
對象的映射和我越來越充滿了陣列,壽而不是Event
對象(這是我想要什麼),我得到NSDictionary
對象。事情是,我有我的Event
類的映射。我的問題是讓RestKit將這些映射到數組中。 我已經嘗試添加一個Event
類tho我沒有運氣的responseDescriptors。
這裏是Player
對象映射
RKObjectMapping* playerMapping = [RKObjectMapping mappingForClass:[Player class]];
[playerMapping addAttributeMappingsFromDictionary:@{
...more here
@"activeEvents" : @"activeEvents"
}];
這裏請求方法
NSURL *taskURL = [NSURL URLWithString:kAppWebApiURLPath];
// Set object manager with base url
RKObjectManager *objectManager = [RKObjectManager sharedManager];
objectManager = [RKObjectManager managerWithBaseURL:taskURL];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager.HTTPClient setDefaultHeader:@"Authorization" value:kAppWebAPIKey];
[objectManager.HTTPClient setDefaultHeader:@"Content-Type" value:@"application/json"];
RKRequestDescriptor * requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[MappingProvider inverseLoginMapping] objectClass:[LoginInfo class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];
RKResponseDescriptor *playersResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider playerMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"players" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:playersResponseDescriptor];
NSLog(@"%@",loginInfo.iOSDeviceToken);
[objectManager postObject:loginInfo path:kAppWebApiLoginPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{}...
更新,我現在需要一個步驟映射進一步的,我的選手對象包含事件的數組,我成功映射使用
[playerMapping addRelationshipMappingWithSourceKeyPath:@"activeEvents" mapping:[MappingProvider eventMapping]];
但現在每個這些Eve nt對象包含一系列玩家,所以它就像玩家 - >事件 - >玩家。
下面是兩個事件和球員的對象映射:
RKObjectMapping* eventMapping = [RKObjectMapping mappingForClass:[Event class]];
[eventMapping addAttributeMappingsFromDictionary:@{
@"stuffhere" : @"stuffz"
}];
RKObjectMapping* playerMapping = [RKObjectMapping mappingForClass:[Player class]];
[playerMapping addAttributeMappingsFromDictionary:@{
@"name": @"name",
@"activeEvents" : @"activeEvents"
}];
[eventMapping addRelationshipMappingWithSourceKeyPath:@"activeEvents/players" mapping:playerMapping];
現在我沒有得到一個遞歸函數,但我怎麼在代碼中陳述,使JSON陣列的這種關係 映射分配給我的本地數組屬性?
它給我一個Event對象,它的所有屬性都是null。有任何想法嗎 ?警告:映射嵌套對象失敗:(空) – yogi
這將表明你的'eventMapping'是錯誤的。你需要顯示它和JSON ... – Wain
是的,我有點偏離。修復它,並得到它運行:),thx的傢伙。派對上的派對:P – yogi