在RESTkit中为JSON数组中的对象指定特定映射?

时间:2012-02-14 12:15:58

标签: ios json restkit

当我请求UserProfile对象列表时,我从django-rest-framework收到此JSON:

[
    {
        "gender": 1, 
        "show_gender": true, 
        "show_real_name": true
    }, 
    {
        "gender": 2, 
        "show_gender": true, 
        "show_real_name": true
    }
]

但我不知道如何使用Restkit配置我的映射。 Restkit似乎期望dictionary instead of a list,因为它似乎使用字典的键作为“KeyPath”来识别必要的映射。有没有办法手动指定接收对象的映射?

1 个答案:

答案 0 :(得分:2)

知道了!

// define a mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[ERUser class]];
[userMapping mapAttributes:
 @"gender",
 @"show_gender",
 @"show_real_name",
 nil];

// add the mapping anonymously
[objectManager.mappingProvider addObjectMapping:userMapping];

// tell the mapping provider which mapping to use
RKObjectMapping* articleMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[ERUser class]];
[objectManager loadObjectsAtResourcePath:@"/users" objectMapping:articleMapping delegate:self];