Note that there are some explanatory texts on larger screens.

plurals
  1. POObject mapping with RestKit
    text
    copied!<p>I am fairly new to RestKit framework. I am facing difficulty in parsing some JSON response.</p> <pre><code>{ "success": "1", "loginStatus": "Success", "sessionToken": "NTE5YTg5NjE4ZDIxYzQ3YzMUybdtsUU8989GVmaW5lZHwxMzY5ODE0MTc4Mzgw", "user": { "ActivationMethod": false, "ActivationToken": "iey4C5E3iMt7mXZL", "Active": true, "FirstName": "John", "LastName": "Doe", } } </code></pre> <p>This is response of my Login web service.</p> <p>What I want is to have value of sessionToken, FirstName and LastName to be mapped in one class. For this I have created a class User</p> <pre><code>@interface User : NSObject @property (nonatomic, strong) NSString* firstName; @property (nonatomic, strong) NSString* lastName; @property (nonatomic, strong) NSString* sessionToken; @end </code></pre> <p>For the mapping I have tried following code :</p> <pre><code> RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class]]; [mapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"firstName", @"LastName": @"lastName", @"sessionToken" : @"sessionToken", }]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"/login" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:responseDescriptor]; </code></pre> <p>Using this I getting values of Lastname and Firstname as null. </p> <p>The url is http://[BASE URL]/login</p> <p>I have gone through this <a href="https://github.com/RestKit/RestKit/wiki/Object-mapping" rel="nofollow">link</a> on object mapping but could resolve this issue.</p> <p>Any pointers in this regard will be very helpful</p> <p>Regards,</p> <p>tek3</p> <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em></strong> EDIT <strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>****</strong></p> <p>I am now able to get the values using code below.</p> <pre><code> RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class] ]; [userMapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"firstName", @"LastName": @"lastName", }]; RKObjectMapping* loginMapping = [RKObjectMapping mappingForClass:[Login class] ]; [loginMapping addAttributeMappingsFromDictionary:@{ @"sessionToken": @"sessionToken", }]; [loginMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping pathPattern:@"/login" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; </code></pre> <p>But I still have one confusion. I had to create another Login as</p> <pre><code>@interface Login : NSObject @property (nonatomic, strong) NSString* sessionToken; @property (nonatomic, strong) User* user; @end </code></pre> <p>Login is having an object of User class as property. Is there any way that I get values of all the keys in JSON response by simply creating a single object and not multiple nested objects?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload