I am having trouble with RESTKIT and mapping the following JSON via RESTKit .20.1. I have a nested JSON array and I am trying to map via the RelationshipMapper. I am able to map the 5 chart_data object successfully but the related nested chart_datatum objects do not map to the ChartDataDetails class. All of the properties are blank.
I would like the following object structure.
Alert Instance
Array of ChartData objects
Each ChartData object should contain 1 ChartDataDetails object <---- this object is blank
Please see the link at the bottom for the screenshot of the XCode Debugger for the current state.
Can someone please provide some assistance?
Thanks, G
{
"alert_instance": {
"alert_template_id": 1,
"alert_time_period": "2013-05-29",
"chart_data": [
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "provider_id",
"datatype": 1,
"chart_data_value": "1"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Provider_name",
"datatype": 2,
"chart_data_value": "Stubbs, Drew"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Posting_date",
"datatype": 3,
"chart_data_value": "05/12/2013"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Charges",
"datatype": 5,
"chart_data_value": "229"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Payments",
"datatype": 5,
"chart_data_value": "-1023.11"
}
}
]
}
Model classes
AlertInstance.h
@interface AlertInstance : NSObject
@property (nonatomic, copy) NSNumber* alertTemplateId;
@property (nonatomic, copy) NSDate* alertTimePeriod;
@property (nonatomic, retain) NSMutableArray* chartData;
@end
ChartData.h
@interface ChartData : NSObject
@property (nonatomic, retain) ChartDataDetails *chartDataDetails;
@end
ChartDataDetails.h
@interface ChartDataDetails : NSObject
@property (nonatomic, copy) NSNumber* chartDataId;
@property (nonatomic, copy) NSString* chartDataName;
@property (nonatomic, copy) NSNumber* chartDataType;
@property (nonatomic, copy) NSNumber* chartDataValue;
@end
code for mappings..
RKObjectMapping* chartDataDetailMapping = [RKObjectMapping mappingForClass:[ChartDataDetails class]];
[chartDataDetailMapping addAttributeMappingsFromDictionary:@{
@"chart_data_id": @"chartDataId",
@"chart_data_name": @"chartDataName",
@"datatype": @"chartDataType",
@"chart_data_value": @"chartDataValue",
}];
RKObjectMapping* chartDataMapping = [RKObjectMapping mappingForClass:[ChartData class]];
[chartDataMapping addAttributeMappingsFromDictionary:@{
@"chart_data":@"chartData"}];
RKObjectMapping* alertInstanceMapping = [RKObjectMapping mappingForClass:[AlertInstance class]];
[alertInstanceMapping addAttributeMappingsFromDictionary:@{
@"alert_template_id": @"alertTemplateId",
@"alert_time_period": @"alertTimePeriod",
}];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_data"
toKeyPath:@"chartData"
withMapping:chartDataMapping]];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_datum"
toKeyPath:@"chartDataDetails"
withMapping:chartDataDetailMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:alertInstanceMapping
pathPattern:nil
keyPath:@"alert_instance"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:@"http://ideainnovationsmobiledev1.cloudapp.net:3000/alert_instances/1.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
link to screen shot of the debugger
https://docs.google.com/file/d/0Bwaj6O1rXiKaeFIwT2s4Rm1Gb00/edit?usp=sharing
EDIT:
Ok I figured it out.
I needed to chain the ChartDetailsMapping to the ChartData as follows.
[chartDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_datum"
toKeyPath:@"chartDataDetails"
withMapping:chartDataDetailMapping]];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_data"
toKeyPath:@"chartData"
withMapping:chartDataMapping]];