我在使用 RESTKIT 并通过 RESTKit .20.1 映射以下 JSON 时遇到问题。我有一个嵌套的 JSON 数组,我正在尝试通过关系映射器进行映射。我能够成功映射 5 个 chart_data 对象,但相关的嵌套 chart_datatum 对象没有映射到 ChartDataDetails 类。所有属性都是空白的。
我想要以下对象结构。
警报实例
ChartData 对象数组
每个 ChartData 对象应包含 1 个 ChartDataDetails 对象 <---- 此对象为空白
有关当前状态的 XCode 调试器屏幕截图,请参阅底部的链接。
有人可以提供一些帮助吗?
谢谢,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"
}
}
]
}
模型类
警报实例.h
@interface AlertInstance : NSObject
@property (nonatomic, copy) NSNumber* alertTemplateId;
@property (nonatomic, copy) NSDate* alertTimePeriod;
@property (nonatomic, retain) NSMutableArray* chartData;
@end
图表数据.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]];