RestKit:具有数组的动态嵌套属性 [英] RestKit: Dynamic Nested Attributes with an Array
问题描述
我正在努力寻找一种将JSON映射到RestKit的方法.这是我正在查看的示例:
I'm struggling to find a way to map some JSON into RestKit. This is an example of what I'm looking at:
"results":{
"Test1":[
{
"id":1,
"name":"Test 1 here.",
"language":"English",
"type: "Test1"
}
],
"Test2":[
{
"id":3,
"name":"Another test 2",
"language":"English",
"type":"Test2"
},
{
"id":8,
"name":"More test 2",
"language":"English",
"type":"Test2"
},
{
"id":49,
"name":"foo",
"language":"English",
"type":"Test2"
}
]
}
理想情况下,JSON不会包含类型"的额外冗余层作为键,但这就是生命.
Ideally, the JSON wouldn't include the extra redundant layer of "type" as a key, but such is life.
我希望RestKit在结果"类型下返回4个对象:
I'd want RestKit to return 4 objects under "results" of type:
@interface Test : NSObject
@property (nonatomic, copy) NSNumber *testId;
@property (nonatomic, copy) NSString *testName;
@property (nonatomic, copy) NSString *testLanguage;
@property (nonatomic, copy) NSString *testType;
我尝试了不同的映射组合,例如:
I've tried different combinations of mappings such as:
RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[Test class]];
testMapping.forceCollectionMapping = YES;
[testMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"testType"];
[testMapping addAttributeMappingsFromDictionary:@{
@"(testType).id": @"testId",
@"(testType).name": @"testName",
@"(testType).language": @"testLanguage",
}];
但是它仍然失败,因为它不是"type" JSON键下的单个对象-它是Test对象的数组.
but it still fails because its not a single object under the "type" JSON key - it is an array of Test objects.
有没有办法在RestKit中表示此映射?或者,如果不能,则可以覆盖某些回调函数,以便我可以使其正常工作?不幸的是,我无法更改来自服务器的JSON数据
Is there a way to represent this mapping in RestKit? Or if not, be able to override some callback functions so I can make it work? Unfortunately, I can't change the JSON data coming from the server
推荐答案
我最好的选择是创建一个响应描述符,该描述符的键路径为@"results"
,并且
I'd say your best bet is to create a response descriptor with a key path of @"results"
and a dynamic mapping. That mapping would return a mapping which maps into an NSDictionary
and which has a number of relationships defined. This dictionary is just a container to facilitate the other mappings (the relationships).
通过使用testMapping
迭代提供给动态映射的表示的键并在每次迭代中创建一个关系来创建关系,但是没有addAttributeMappingFromKeyOfRepresentationToAttribute
,因为您现在可以使用直接属性访问(和内部type
属性).
The relationships are created by iterating the keys of the representation provided to the dynamic mapping and creating one relationship per iteration, using the testMapping
, but without the addAttributeMappingFromKeyOfRepresentationToAttribute
as you can now use direct attribute access (and the inner type
attribute).
使用setObjectMappingForRepresentationBlock:
,您的块具有representation
,在您的情况下,这是反序列化JSON的NSDictionary
.在该块内,您可以像平常一样创建一个映射,但是其内容基于字典中的键.
Using setObjectMappingForRepresentationBlock:
, your block is provided with the representation
, which in your case is an NSDictionary
of the deserialised JSON. Inside the block you create a mapping just as you usually would, but with the contents based on the keys in the dictionary.
RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[Test class]];
[testMapping addAttributeMappingsFromDictionary:@{ @"id": @"testId", @"name": @"testName" }];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
RKObjectMapping *testListMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
for (NSString *key in representation) {
[testListMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeypath:key toKeyPath:key withMapping:testMapping];
}
return testListMapping;
}];
这篇关于RestKit:具有数组的动态嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!