I am working on an Objective-C project that uses the RestKit framework to parse JSON responses. Now I need help with object mapping settings for the following case: JSON response:
{
"data": {
"SOME.API.Auth": {
"maxVersion": 2,
"minVersion": 1,
"path": "auth.cgi"
},
"SOME.Station": {
"maxVersion": 1,
"minVersion": 1,
"path": "Station/task.cgi"
}
},
"success": true
}
and the following objects:
@interface Response : NSObject
@property (strong, nonatomic) NSArray *data;
@property (assign, nonatomic) BOOL success;
@end
@interface SomeAPIInfo : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *path;
@property (strong, nonatomic) NSString *minVersion;
@property (strong, nonatomic) NSString *maxVersion;
@end
And here are my display settings:
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"success": @"success"}];
RKObjectMapping *dataObjectMapping = [RKObjectMapping mappingForClass:[SomeAPIInfo class]];
dataObjectMapping.forceCollectionMapping = YES;
[dataObjectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[dataObjectMapping addAttributeMappingsFromDictionary:@{
@"(name).path": @"path",
@"(name).minVersion": @"minVersion",
@"(name).maxVersion": @"maxVersion"}];
[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data"
toKeyPath:@"data"
withMapping:dataObjectMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[_objectManager addResponseDescriptor:responseDescriptor];
The problem is that the "data" object is not displayed correctly: NSArray * data; populated with "SomeAPIInfo" objects. the name is correctly populated, but other values ββ(path, maxVersion, minVersion) are empty.
What am I doing wrong? Is there any other way to display a βdataβ object? Perhaps directly in the NSDictionary, so "Some.API.Auth" will be the key, and "SomeAPIInfo" will be the object (without the "name" property).
Thanks for the help!
: , - ( "SOME.API.Auth" ).
RKMappingOperation.m:
- (NSArray *)simpleAttributeMappings
{
NSMutableArray *mappings = [NSMutableArray array];
for (RKAttributeMapping *mapping in self.nestedAttributeMappings) {
if ([mapping.sourceKeyPath rangeOfString:@"."].location == NSNotFound) {
[mappings addObject:mapping];
}
}