我遇到RESTKIT问题,并通过RESTKit .20.1映射以下JSON。我有一个嵌套的JSON数组,我试图通过RelationshipMapper进行映射。我能够成功映射5个chart_data对象,但相关的嵌套chart_datatum对象不会映射到ChartDataDetails类。所有属性都是空白的。
我想要以下对象结构。
警报实例
ChartData对象数组
每个ChartData对象应包含1个ChartDataDetails对象< ----此对象为空
请参阅底部的链接,了解当前状态的XCode调试器的屏幕截图。
有人可以提供一些帮助吗?
谢谢, ģ
{
"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"
}
}
]
}
模型类
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
映射代码..
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)
链接到调试器的屏幕截图
https://docs.google.com/file/d/0Bwaj6O1rXiKaeFIwT2s4Rm1Gb00/edit?usp=sharing
编辑:
好的我明白了。
我需要将ChartDetailsMapping链接到ChartData,如下所示。
[chartDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_datum"
toKeyPath:@"chartDataDetails"
withMapping:chartDataDetailMapping]];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"chart_data"
toKeyPath:@"chartData"
withMapping:chartDataMapping]];