我必须映射这个结构,但我不知道如何识别“br.myservice.com”部分,因为它会改变,我无法构建常规的 RKObjectMapping
,因为它使用固定字符串。
{ "objects": {
"br.myservice.com": {
"url": "http://br.myservice.com",
"name": "Brazil",
"flag": "br",
"countries": ["br"]
},
"us.myservice.com": {
"url": "http://us.myservice.com",
"name": "United States",
"flag": "us",
"countries": ["us"]
}
}
最佳答案
您需要使用 addAttributeMappingFromKeyOfRepresentationToAttribute:
像这样:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[MyObject class]];
mapping.forceCollectionMapping = YES;
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"host"];
[mapping addAttributeMappingsFromDictionary:@{
@"(host).url": @"url",
@"(host).name": @"name",
... and so on ....
}];
关于ios - Restkit — 如何映射具有可变键名的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20495517/