Search code examples
iosparsingmappingrestkit

Parsing a simple JSON object with restkit


I'm using restkit on IOS. I'm having trouble parsing a simple json object.

I have a simple json object returned:

{"_id"=>"537c235189d50fabcc000009",
 "about"=>"nice",
 "headline"=>"looking",
 "access_token"=>
  "$2a$10$oZ4IiaVBxHhc1qGeBoZv1uYonBM3Qb5Y010rTkUOynDZIGdGagqJy"}

My setup:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureRestKit];
    [self loadVenues];
}

- (void)loadVenues
{
    NSString *clientToken = SNAPTOKEN;
    NSDictionary *profile = @{@"os": @"iOS",
                              @"device_token": @"first_token",
                              @"password":@"password",
                              @"email":@"email",
                              @"headline":@"hello world"};

    NSDictionary *queryParams = @{@"token" : clientToken,
                                  @"profile" : profile};

    [[RKObjectManager sharedManager] postObject: nil
                                           path: @"/profiles/new"
                                           parameters:queryParams
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                  NSLog(@"log%@", mappingResult );
                                                  _profile = mappingResult.array;
                                              }
                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                  NSLog(@"What do you mean by 'there is no coffee?': %@", error);
                                              }];
}

- (void)configureRestKit
{
    // initialize AFNetworking HTTPClient
    NSURL *baseURL = [NSURL URLWithString:@"https://airimg.com"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

    // initialize RestKit
    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

    // setup object mappings
    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Profile class]];
    [venueMapping addAttributeMappingsFromDictionary:@{ @"_id": @"about", @"access_token": @"headline" }];

    // register mappings with the provider using a response descriptor
    RKResponseDescriptor *responseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                                 method:RKRequestMethodPOST
                                            pathPattern:@"/profiles/new"
                                                keyPath:@"response"
                                            statusCodes:[NSIndexSet indexSetWithIndex:200]];

    [objectManager addResponseDescriptor:responseDescriptor];

}

I have the following error:

NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: response
The representation inputted to the mapper was found to contain nested object representations at the following key paths: _id, about, access_token, headline
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

Solution

  • The main problem is that your response descriptor uses a key path of response and your JSON doesn't include that key at the top level. So, set the key path to 'nil'.

    Also, your view controller should store the reference to the objectManager and use it again later. Currently you use [RKObjectManager sharedManager] and that returns the first object manager that was ever created so as soon as you have multiple view controllers each creating their own object manager you will have issues.

    Of course, the view controllers shouldn't necessarily be creating individual object managers...