Reputation: 2378
I am confused with RKObjectMapping in new restkit 10.0. I have invoked my webservice with RKClient and got response in the form of JSON. Now I want to map the JSON string with my User class . How to do it ?
{"id":2,"displayName":"Giri Bhushan","roles":null,"username":"FBUser-123456","password":"dddddddddddddddd=\r\n","email":"[email protected]","gender":"M","birthDate":null,"friends":null,"bloodGroup":null,"firstName":null,"lastName":null,"phoneNumber":null}
I have tried with below code :
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"id" toAttribute:@"userId"];
[userMapping mapKeyPath:@"displayName" toAttribute:@"displayName"];
[userMapping mapKeyPath:@"userName" toAttribute:@"userName"];
[userMapping mapKeyPath:@"password" toAttribute:@"password"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"];
[userMapping mapKeyPath:@"birthDate" toAttribute:@"birthDate"];
[userMapping mapKeyPath:@"bloodGroup" toAttribute:@"bloodGroup"];
[userMapping mapKeyPath:@"firstName" toAttribute:@"firstName"];
[userMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[userMapping mapKeyPath:@"phoneNumber" toAttribute:@"phoneNumber"];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"];
But, how do I map the JSON string with RKObjectMapping?
Upvotes: 0
Views: 406
Reputation: 636
You are missing this line:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"" delegate:self];
then implement its delegate method like:
(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{
NSLog(@"Objects Retrieved: %d\n%@", objects.count, objects);
YourObject* obj = [objects objectAtIndex:0];
NSString* info = [NSString stringWithFormat:
@"Value 1 is %@\n"
@"Value 2 is %@\n"
@"Value 3 is: %@",
[obj value1],
[obj value2],
[obj value3];
}`
Hope this works for you. XD
Upvotes: 0
Reputation: 1387
If you have users at the root level of your JSON, the following should work:
RKObjectManager *manager = [RKObjectManager sharedManager];
RKManagedObjectMapping *userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:manager.objectStore];
[userMapping mapKeyPath:@"id" toAttribute:@"userId"];
[userMapping mapAttributes:@"displayName",@"userName",@"password", nil]; // Add other attributes
[manager.mappingProvider setMapping:userMapping forKeyPath:@""];
Upvotes: 0