RestKit:如何为不同的键名进行映射 [英] RestKit: how to make a mapping for varying key names
问题描述
我正在尝试为有点怪异的结构化JSON创建动态映射. 我有映射到对象的数组"之类的稀薄对象,以便数组索引 按键,例如:
I'm trying to make a dynamic mapping for a bit weird structured JSON. I have "array mapped to object" sort of thins so that array indices ake keys e.g.:
{
"0": {object},
"1": {another object},
"2": {yet another object},
...
}
所有对象都是相同的类型,因此可以使用相同的对象进行解析 映射,但是如何处理不同的键名?
All objects are of the same type so they can be parsed using the same mapping, but how to deal with varying key names?
推荐答案
He walks through an example (copied here) with the JSON:
{ "blake": {
"email": "blake@restkit.org",
"favorite_animal": "Monkey"
}
}
对应于User类:
@interface User : NSObject
@property (nonatomic, retain) NSString* email
@property (nonatomic, retain) NSString* username;
@property (nonatomic, retain) NSString* favoriteAnimal;
@end
您会注意到,username属性对应于JSON的 key .
Which, you'll notice, the username property corresponds to the key of the JSON.
为了进行映射,他使用特殊的括号语法来表明它们本身就是一个属性:
In order to map it, he uses a special parenthesis syntax to indicate that they key itself is a property:
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ];
[mapping mapKeyOfNestedDictionaryToAttribute:@"username"];
[mapping mapFromKeyPath:@"(username).email" toAttribute:"email"];
[mapping mapFromKeyPath:@"(username).favorite_animal" toAttribute:"favoriteAnimal"];
希望这会有所帮助!
这篇关于RestKit:如何为不同的键名进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!