I'm having an issue mapping the following JSON payload using RestKit 0.20
I am trying to map the Product and its Attachments as an array with the following objects. I am not using RKEntityMapping, simply RKObjectMappings as this app is not using Core Data. I have consulted the documentation on Object Mappings on the RestKit wiki, and looked at the test cases in the source code for a similar setup but no luck.
{
"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;
Here are my object mappings and my 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]];
I am getting a valid response, but the odd thing is its only mapping the description attribute for the Product, not even its name!
2013-03-05 18:39:49.775 iOS[38965:c07] Loaded results: <RKMappingResult: 0x96a8950, results={
"Result.Product" = "Its EVIL!";
When I add a second response descriptor, drilling down into Result.Product.Attachments, I can get an array of attachments no problem with only the attachments response descriptor. Any help would be greatly appreciated, I've been banging my head on this since Sunday afternoon. Apologies for the wall of code, but its hard to describe RestKit setups fully without all the pieces. Thank you.
To facilitate and make the answer more visible to users:
@Jorge Chao's comment(answer) on own question:
Answer of this Question:
I have fixed the problem. First, the RKMappingResult
that I have pasted is incorrect. It IS mapping an item, but it is only displaying one of its attributes. To make it work I removed the RKRelationshipMapping
and the NSAray
in the product class and just added a second RKResponseDescriptor
for the attachments.
Everything appears correctly in the RKMappingResult
, the array just contains heterogeneous elements unfortunately. It's a hack, but it finally fixed my problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With