上下文
// 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 / Class / Mapping代码。我已经使用了一段时间,一切都按预期工作。
今天我遇到了JSON中的密钥包含括号并导致映射失败的情况。我有办法让这个工作吗?
谢谢!
示例:
"Name (haha this will break)" : {
"param_1":"value_1",
"param_2":"value_2"
}
答案 0 :(得分:0)
我认为RestKit
感到困惑,因为它不知道在映射时使用哪个括号。所以我的猜测是用大括号替换它们:
[mapping addAttributeMappingsFromDictionary:@{
@"{name}.param_1":@"param1",
@"{name}.param_2":@"param2"
}];
如果有效,请告诉我。
答案 1 :(得分:0)
<强>解决方案:强>
更新RKStringByReplacingUnderscoresWithBraces
中的RKPropertyMapping
方法,不要用括号替换括号,然后确保在属性映射中使用大括号。
// 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
在上面的RestKit文档中,当您尝试将某个键映射到属性时,系统会指示您使用addAttributeMappingFromKeyOfRepresentationToAttribute
,然后使用括号映射嵌套键以表示您的属性,后跟一个&#39;。&# 39;和你的嵌套密钥。
在RestKit中,它从addAttributeMappingFromKeyOfRepresentationToAttribute
属性映射执行映射后,循环遍历所有已定义的属性映射,以使用键中的实际值替换占位符,以便进行其他映射操作。在此过程中,它会创建一个新的RKPropertyMapping
对象并设置它的sourceKeyPath和destinationKeyPath。这些属性的属性设置器获取值并用括号替换括号,然后RestKit找不到带括号的值的错误映射。
示例上下文:
// JSON
{ "blake": {
"email": "blake@restkit.org",
"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": "blake@restkit.org",
"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"