我有一个问题,无法映射json的嵌套对象。问题在于“容量”键。
这是错误:
restkit.object_mapping:RKMappingOperation.m:830 Did not find mappable relationship value keyPath 'capacities'
我知道问题在于映射本身,但是无法想象:
RKObjectMapping *capacityMapping = [RKObjectMapping mappingForClass:[Capacity class]];
[capacityMapping addAttributeMappingsFromDictionary:@{ @"capacityText" : @"capacityText",
@"priceDescriptionText" : @"priceDescriptionText",
@"priceText" : @"priceText" }];
RKObjectMapping *colorsMapping = [RKObjectMapping mappingForClass:[Colors class]];
[colorsMapping addAttributeMappingsFromDictionary:@{ @"ID" : @"idNum",
@"Name" : @"name",
@"colorHex" : @"colorHex",
@"imageUrl" : @"imageURL" }];
RKObjectMapping *deviceDataMapping = [RKObjectMapping mappingForClass:[DeviceData class]];
[deviceDataMapping addAttributeMappingsFromDictionary:@{ @"device.ID" : @"idNum",
@"device.Name" : @"name",
@"device.additionalFeatures" : @"additionalFeatures",
@"device.deviceName" : @"deviceName",
@"device.mainFeatures" : @"mainFeatures",
@"device.supportPagesLinks" : @"supportPagesLinks",
@"device.whatsInTheKit" : @"whatsInTheKit" }];
[deviceDataMapping addRelationshipMappingWithSourceKeyPath:@"capacities" mapping:capacityMapping];
[capacityMapping addRelationshipMappingWithSourceKeyPath:@"colors" mapping:colorsMapping];
[deviceDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"responseError"
toKeyPath:@"responseError"
withMapping:errorMapping]];
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:deviceDataMapping
method:RKRequestMethodGET
pathPattern:@"devices/:boneID"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
这是DeviceData
类:
@interface DeviceData : NSObject
@property (nonatomic) ResponseError *responseError;
@property (copy, nonatomic) NSString *idNum;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *additionalFeatures;
@property (copy, nonatomic) NSString *deviceName;
@property (copy, nonatomic) NSString *mainFeatures;
@property (copy, nonatomic) NSString *supportPagesLinks;
@property (copy, nonatomic) NSString *whatsInTheKit;
@property (nonatomic) NSArray *capacities;
@property (assign, nonatomic) NSInteger boneID;
@end
这是Capacity
类:
@interface Capacity : NSObject
@property (copy, nonatomic) NSString *capacityText;
@property (nonatomic) NSArray *colors;
@property (copy, nonatomic) NSString *priceDescriptionText;
@property (copy, nonatomic) NSString *priceText;
@end
这是Colors
类:
@interface Colors : NSObject
@property (copy, nonatomic) NSString *idNum;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *colorHex;
@property (copy, nonatomic) NSString *imageURL;
@end
这是json:
{
"responseError": null,
"device": {
"ID": null,
"Name": null,
"additionalFeatures": "additional features text",
"capacities": [
{
"capacityText": "16GB",
"colors": [
{
"ID": null,
"Name": null,
"colorHex": "#a68f76",
"imageUrl": "iphone_5s_black.png"
},
{
"ID": null,
"Name": null,
"colorHex": "#a9a9a9",
"imageUrl": "iphone_5s_black.png"
},
{
"ID": null,
"Name": null,
"colorHex": "#616065",
"imageUrl": "iphone_5s_black.png"
}
],
"priceDescriptionText": “iPhone 5S",
"priceText": "750$"
},{
"capacityText": “32GB",
"colors": [
{
"ID": null,
"Name": null,
"colorHex": "#a68f76",
"imageUrl": "iphone_5s_black.png"
},
{
"ID": null,
"Name": null,
"colorHex": "#a9a9a9",
"imageUrl": “iphone_5s_black.png"
},
{
"ID": null,
"Name": null,
"colorHex": "#616065",
"imageUrl": “iphone_5s_black.png"
}
],
"priceDescriptionText": “iPhone 5S",
"priceText": "750$"
}
],
"deviceName": "iPhone 5s",
"mainFeatures": “some main features text",
"supportPagesLinks": [
{
"linkText": “restore",
"linkUrl": “restore.pdf"
}],
"whatsInTheKit": "what inside the kit text"
}
}
答案 0 :(得分:5)
因为您的响应描述符具有nil密钥路径,并且您在device.xxx
的所有源密钥路径中使用deviceMapping
,所以您需要具有显式关系映射,以便您可以指定源和目标密钥路径:
RKRelationshipMapping *capacitiesMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"device.capacities" toKeyPath:@"capacities" withMapping:capacityMapping];
[deviceDataMapping addPropertyMapping:capacitiesMapping];