RestKit 0.20:使用JSON中的动态展平层次结构

时间:2013-05-31 06:37:43

标签: ios core-data restkit-0.20

我正在使用RestKit 0.20,并且在将包含动态密钥的层次结构映射到一个对象时遇到问题。

JSON看起来像这样:

{
    "id": 42,
    "name": "Name of this entity",
    "specialDataMap": {
        "2091:10": {
            "id": 2091,
            "type": "10",
            "value": "1'509.49",
            "name": "Name of special data type 10"
        },
        "2091:02" {
            "id": 2091,
            "type": "02",
            "value": "5.5543",
            "name": "Name of special data type 02"
        }
    }
}

并且应该使用RestKit映射到这样的对象:

@interface InfoPoint : NSManagedObject

@property (nonatomic, retrain) NSString* identifier;
@property (nonatomic, retrain) NSString* name;
@property (nonatomic, retrain) NSString* valueOfType10;
@property (nonatomic, retrain) NSString* valueOfType02;

@end

如您所见,我不想创建关系并将特殊数据存储到单独的对象中。它没有意义。 我想像所有其他属性一样将嵌套属性分配到InfoPoint对象中。通常这将与嵌套对象的键路径一起使用,但此路径包含动态部分:“2091:10”是id和id可能更改的类型的组合(不是我的'原创'想法,但我有消费它。)

我已阅读RestKit文档中的Handling Dynamic Nesting Attributes。但是我没有找到它是否以及如何与嵌套属性一起工作。

-----作为对评论/问题的回复添加:----

我也和Dynamic Object Mapping一起尝试过。但它没有用,因为RestKit似乎与@“自我”目的地有问题:

RKEntityMapping* type10Mapping = [RKEntityMapping mappingForEntityForName:@"InfoPoint" inManagedObjectStore:objectStore];
[type10Mapping addAttributeMappingsFromDictionary:@{
 @"value": @"valueOfType10"}];

RKDynamicMapping* dynamicMapping = [[RKDynamicMapping alloc] init];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"specialDataMap" toKeyPath:@"self" withMapping:dynamicMapping]];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
    if ([[representation valueForKeyPath:@"type"] isEqualToString:@"10"]) {
        return type10Mapping;
    }
    return nil;
}]; 

1 个答案:

答案 0 :(得分:1)

您对动态映射的尝试不正确。您应该为响应描述符提供动态映射。然后,动态映射应基于它在响应中找到的键创建并返回适当的映射。像(自由写手)的东西:

RKDynamicMapping* dynamicMapping = [[RKDynamicMapping alloc] init];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

    RKEntityMapping* typeMapping = [RKEntityMapping mappingForEntityForName:@"InfoPoint" inManagedObjectStore:objectStore];
    [typeMapping addAttributeMappingsFromDictionary:@{
        @"id" : @"identifier",
        @"name": @"name",}];

    NSDictionary *types = [representation valueForKeyPath:@"specialDataMap"];

    for (NSString *key in types) {
        NSDictionary *type = [[types objectForKey:key] objectForKey:@"type"];
        if ([type isEqualToString:@"10"]) {
            [typeMapping addAttributeMappingsFromDictionary:@{
                [NSString stringWithFormat:@"specialDataMap.%@.value", key]: @"valueOfType10"}];
        } else if ...
    }

    return typeMapping;
}]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping
                                                                                   pathPattern:...
                                                                                       keyPath:nil
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];