Reputation: 1042
My JSON has the following structure:
{
"<Person>k__BackingField" = {
"_email" = "[email protected]";
"_emailUpdate" = 1;
"_firstName" = "First";
"_lastName" = "Last";
};
"<SharedKey>k__BackingField" = "some-long-random-string";
}
I'm using restkit to login as this person. It creates the Post, and gets that response correctly. My mappings are set up like this:
+(void)ConfigureAPI
{
[super ConfigureAPI];
// Initialize RestKit
[RKObjectManager managerWithBaseURLString: @"www.api_root.com/";
Class<RKParser> parser = [[RKParserRegistry sharedRegistry] parserClassForMIMEType:@"application/json"];
[[RKParserRegistry sharedRegistry] setParserClass:parser forMIMEType:@"text/plain"];
RKObjectMapping *mapping;
// Person
mapping = [RKObjectMapping mappingForClass:[Person class]];
[mapping mapKeyPath:@"_email" toAttribute:@"email"];
[mapping mapKeyPath:@"_emailUpdate" toAttribute:@"emailUpdate"];
[mapping mapKeyPath:@"_firstName" toAttribute:@"firstName"];
[mapping mapKeyPath:@"_lastName" toAttribute:@"lastName"];
[[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:@"<Person>k__BackingField"];
}
When I run the application and call the function that gets the JSON response, I get the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7b41800> valueForUndefinedKey:]: this class is not key value coding-compliant for the key <Person>k__BackingField.'
Anybody know why I'm getting this? everything seems to be set up correctly
EDIT
In case it helps, this is how I'm handling the response:
// when you get the response...
request.onDidLoadResponse = ^(RKResponse *response){
RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:[response bodyAsString] mappingProvider:[RKObjectManager sharedManager].mappingProvider];
Person *person = nil;
RKObjectMappingResult *mappingResult = [mapper performMapping];
person = [mappingResult asObject];
};
Upvotes: 0
Views: 243
Reputation: 6626
You are not converting the response string into a JSON object before passing it to the RKObjectMapper. You were passing in the string value of the response which does not respond to valueForKey:
which explains the exception you were getting. You can easily convert it into an object like this:
request.onDidLoadResponse = ^(RKResponse *response){
NSError *error;
id jsonObject = [response parsedBody:&error];
if(error)
NSLog(@"Error parsing JSON: %@", error.localizedDescription);
else {
RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:jsonObject mappingProvider:[RKObjectManager sharedManager].mappingProvider];
Person *person = nil;
RKObjectMappingResult *mappingResult = [mapper performMapping];
person = [mappingResult asObject];
}
};
Upvotes: 1