我在使用 RestKit 0.20 映射以下 JSON 有效负载时遇到问题
我正在尝试将产品及其附件映射为具有以下对象的数组。我没有使用 RKEntityMapping,只是使用 RKObjectMappings,因为这个应用程序没有使用 Core Data。我在 RestKit wiki 上查阅了有关对象映射的文档,并查看了源代码中的测试用例以进行类似的设置,但没有运气。
{
"Result": {
"Product": {
"Name": "Evil Stuff",
"Description": "Its EVIL!",
"Attachments": [
{
"Id": "c4277b8f-5930-4fee-a166-b5f311d3a353",
"Name": "commands_to_recreate_db.txt",
"Type": "Contraindications"
},
{
"Id": "be4d2e2e-cb3e-48d2-9c7a-fbfddea3a1be",
"Name": "json.txt",
"Type": "Medication Guide"
}
]
},
"Company": {
"Name": "Omni Consumer Products",
"AddressLine1": "100 Evil Dr.",
"AddressLine2": null,
"AddressLine3": null,
"City": "MARS",
"State": null,
"Zip": "10000",
"Country": null,
"PhoneNumber1": "555-555-5555",
"PhoneNumber2": null,
"PhoneNumber3": null,
"FaxNumber": null,
"WebSite": null,
"Email": null
},
"Representatives": []
},
"Messages": [],
"Success": true
}
@interface clinProduct : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSArray *attachments;
@interface clinAttachment : NSObject
@property (strong, nonatomic) NSString *attachmentID;
@property (strong, nonatomic) NSString *attachmentName;
@property (strong, nonatomic) NSString *type;
这是我的对象映射和我的 RKResponseDescriptor
RKObjectMapping *attachmentMapping = [RKObjectMapping mappingForClass:[clinAttachment class]];
[attachmentMapping addAttributeMappingsFromDictionary:@{@"Id" : @"attachmentID",
@"Name" : @"attachmentName",
@"Type" : @"type"}];
RKObjectMapping *productMapping = [RKObjectMapping mappingForClass:[clinProduct class]];
[productMapping addAttributeMappingsFromDictionary:@{@"Description" : @"description",
@"Name" : @"name"}];
RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Attachments"
toKeyPath:@"attachments"
withMapping:attachmentMapping];
[productMapping addPropertyMapping:relationshipMapping];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping
pathPattern:nil
keyPath:@"Result.Product"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
我得到了一个有效的响应,但奇怪的是它只映射了产品的描述属性,甚至没有它的名称!
2013-03-05 18:39:49.775 iOS[38965:c07] Loaded results: <RKMappingResult: 0x96a8950, results={
"Result.Product" = "Its EVIL!";
当我添加第二个响应描述符,深入到 Result.Product.Attachments 时,我可以获得一个附件数组,仅使用附件响应描述符就没有问题。任何帮助将不胜感激,自周日下午以来,我一直在努力解决这个问题。为代码墙道歉,但如果没有所有部分,很难完全描述 RestKit 设置。谢谢你。