问题
I had been parsing my JSON quite nicely but my server just changed on me. My JSON used to look like this:
{
"blobs": [
{
"createdOn": "2012-03-16T15:13:12.551Z",
"description": "Fake description",
"hint": "And a useless hint",
"id": 400,
"name": "Fake CA one",
"publicId": "FF6",
"type": 0
},
{
"createdOn": "2012-03-16T17:33:48.514Z",
"description": "No hint on this one, but it does have a description.",
"hint": "Hint",
"id": 402,
"name": "Second fake one in CA",
"publicId": "FF8",
"type": 0
}
]
}
and my mapping looked like this:
RKObjectMapping* blobMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponse class]];
[blobMapping mapKeyPath:@"name" toAttribute:@"name"];
[blobMapping mapKeyPath:@"id" toAttribute:@"blobId"];
[blobMapping mapKeyPath:@"description" toAttribute:@"description"];
[blobMapping mapKeyPath:@"hint" toAttribute:@"hint"];
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobs"];
Now my server has changed and I get this back:
{
"blobsList": {
"blobs": [
{
"createdOn" :"2012-03-16T15:13:12.551Z",
"description": "Fake description",
"hint": "And a useless hint",
"id": 400,
"name": "Fake CA one",
"publicId": "FF6",
"type": 0
},
{
"createdOn": "2012-03-16T17:33:48.514Z",
"description": "No hint on this one, but it does have a description.",
"hint": "Hint",
"id": 402,
"name": "Second fake one in CA",
"publicId": "FF8",
"type": 0
}
]
}
}
So I added this to my mapping:
RKObjectMapping* blobsListMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponseList class]];
[blobsListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"];
[[RKObjectManager sharedManager].mappingProvider setMapping:blobsListMapping forKeyPath:@"blobsList"];
And are are my Classes:
@interface GetResponseInRegionResponse : NSObject
{
NSString* name;
NSString* blobId;
NSString* description;
NSString* hint;
}
@interface GetResponseInRegionResponseList : NSObject
{
NSArray *blobsList;
}
When I parse this JSON, I get one object that has a JKArray of 2 objects in it, both of those are JKDictionary objects. So clearly that is my data, but it is in JKDictionary form. It never mapped to the GetResponseInRegionResponse class!
From reading the github docs it looks like I want to use a toRelationship method for arrays, but I'm just not seeing where to put it. If I follow the "articles" example and try this:
[blobListMapping mapKeyPath:@"blobs" toAttribute:@"blobsList"];
[blobListMapping mapKeyPath:@"blobs" toRelationship:@"blobsList" withMapping:blobMapping];
I get this exception:
2012-03-19 14:59:53.704 Ferret[8933:16303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath blobsList, one already exists...'
So how can I map an array of complex objects inside my JSON?
I appreciate any help. Thanks!
回答1:
Have you tried only changing
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];
to reflect the changed path to your data array?
回答2:
The accepted answer appears to address a version before v0.2, the current version. My solution looks slightly different, code is pasted below:
RKObjectMapping * eventMapping = [RKObjectMapping mappingForClass:[RKJMEvent class]]; [eventMapping addAttributeMappingsFromDictionary:[RKJMEvent map]];
RKObjectMapping * eventResponseMapping =[RKObjectMapping mappingForClass:[RKJMEventsResponse class]];
[eventResponseMapping addAttributeMappingsFromDictionary:[RKJMEventsResponse map]];
[eventResponseMapping addRelationshipMappingWithSourceKeyPath:@"events" mapping:eventMapping];
RKResponseDescriptor *responseDescriptorEventResponse = [RKResponseDescriptor responseDescriptorWithMapping:eventResponseMapping
pathPattern:API_GET_EVENTS_PATH
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptorEventResponse];
来源:https://stackoverflow.com/questions/9743155/restkit-mapping-with-an-array-of-complex-objects