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"
}
]
}
Ideally, the JSON wouldn't include the extra redundant layer of "type" as a key, but such is life.
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",
}];
but it still fails because its not a single object under the "type" JSON key - it is an array of Test objects.
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