2017-10-19 86 views
1

上下文:与作为属性和关键母密钥RestKit映射中包含括号

// JSON 
"Name_Field" : { 
    "param_1":"value_1", 
    "param_2":"value_2" 
} 

// Class 
class Field { 
    name 
    param1 
    param2 
} 

// Mapping Functionality 
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"]; 
[mapping addAttributeMappingsFromDictionary:@{ 
    @"(name).param_1":@"param1", 
    @"(name).param_2":@"param2" 
}]; 

问题:

我目前与上述JSON /等级/映射代码工作。我已经使用了一段时间,一切都按预期工作。

今天我遇到了JSON中的键包含括号并导致映射失败的场景。有没有办法让这个工作?

谢谢!

例子:

"Name (haha this will break)" : { 
    "param_1":"value_1", 
    "param_2":"value_2" 
} 
+0

你克设置任何错误?或者映射发生时,该字段被忽略? – RaffAl

+0

我没有收到错误。自发布此问题以来,我一直在进行调试,看起来括号的替换方式与用于标注名称的括号相同。即“名字{哈哈这将打破}”。 – egarlock

+0

经过多次调试,我会在这里尝试您的解决方案。 – egarlock

回答

0

解决方案:

更新在RKPropertyMappingRKStringByReplacingUnderscoresWithBraces方法没有用大括号代替括号,然后一定要在你的属性映射到使用大括号。

// RKPropertyMapping 
static NSString *RKStringByReplacingUnderscoresWithBraces(NSString *string) 
{ 
    return string; 
    //return [[string stringByReplacingOccurrencesOfString:@"(" withString:@"{"] stringByReplacingOccurrencesOfString:@")" withString:@"}"]; 
} 


// Attribute Mappings 
[mapping addAttributeMappingsFromDictionary:@{ 
    @"{name}.param_1":@"param1", 
    @"{name}.param_2":@"param2" 
}]; 

说明:

https://github.com/RestKit/RestKit/wiki/Object-mapping#handling-dynamic-nesting-attributes

在当您试图映射一键,你将被要求使用addAttributeMappingFromKeyOfRepresentationToAttribute然后映射使用括号嵌套键性的上述RestKit文档表示您的财产,然后是'。'和你的嵌套键。

在从addAttributeMappingFromKeyOfRepresentationToAttribute属性映射执行映射之后的RestKit中,它循环遍历所有已定义的属性映射,以便用键中的实际值替换占位符,以便执行额外的映射操作。在此过程中,它会创建一个新的RKPropertyMapping对象并将其设置为sourceKeyPath和destinationKeyPath。这些属性的属性设置器取值并用大括号替换括号,然后RestKit不会为带有大括号的值找到不正确的映射。

实施例上下文:

// JSON 
{ "blake": { 
    "email": "[email protected]", 
    "favorite_animal": "Monkey" 
    } 
} 

// Class 
@interface User : NSObject 
@property (nonatomic, copy) NSString* email 
@property (nonatomic, copy) NSString* username; 
@property (nonatomic, copy) NSString* favoriteAnimal; 
@end 

// Mapping 
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ]; 
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"username"]; 
[mapping addAttributeMappingsFromDictionary:@{ 
    @"(username).email": @"email", 
    @"(username).favorite_animal": @"favoriteAnimal" 
}]; 

示例映射:

// Resulting Attribute Mappings 
@"{username}.email": @"email", 
@"{username}.favorite_animal": @"favoriteAnimal" 


// 1. The attribute mapping has been performed for 'blake' > 'username' 
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake' so your further nested attribute mappings can take place 
// 3. Example: @"{username}.email": @"email" 
     sourceKeyPath = @"{username}.email" 
     destinationKeyPath = @"email" 

     * replaces values * 
     sourceKeyPath = @"blake.email" 
     destinationKeyPath = @"email" 

     * creates a new RKPropertyMapping object * 
     RKPropertyMapping 
     - sourceKeyPath = @"blake.email" 
     - destinationKeyPath = @"email" 

示例映射随着括号:

// JSON 
{ "blake (a.k.a GOAT)": { 
    "email": "[email protected]", 
    "favorite_animal": "Monkey" 
    } 
} 

// Resulting Attribute Mappings 
@"{username}.email": @"email", 
@"{username}.favorite_animal": @"favoriteAnimal" 


// 1. The attribute mapping has been performed for 'blake (a.k.a GOAT)' > 'username' 
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake (a.k.a GOAT)' so your further nested attribute mappings can take place 
// 3. Example: @"{username}.email": @"email" 
     sourceKeyPath = @"{username}.email" 
     destinationKeyPath = @"email" 

     * replaces values * 
     sourceKeyPath = @"blake (a.k.a GOAT).email" 
     destinationKeyPath = @"email" 

     * creates a new RKPropertyMapping object * 
     RKPropertyMapping 
     - sourceKeyPath = @"blake {a.k.a GOAT}.email" // DOES NOT MATCH 
     - destinationKeyPath = @"email" 
0

我觉得RestKit会很困惑,因为它不知道哪个括号,而映射使用。所以我的猜测是用大括号代替它们:

[mapping addAttributeMappingsFromDictionary:@{ 
    @"{name}.param_1":@"param1", 
    @"{name}.param_2":@"param2" 
}]; 

让我知道这是否奏效。