Search code examples
iosobjective-ciphonexcoderestkit

RESTful service response isn't mapping to an object - RESTKit


I have a JSON response from REST service I am using RESTKit and its not getting mapped , below is the source for the same

RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
    [userMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"strCode" toKeyPath:@"UserCode"]];



    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping
                                                                                   objectClass:[User class]
                                                                                   rootKeyPath:nil method:RKRequestMethodPOST];

    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];

    [responseMapping addAttributeMappingsFromDictionary:@{ @"Id":@"Id",@"UserCode":@"strCode",@"FirstName": @"strFname", @"LastName": @"strLname",@"Email":@"strEmail",@"PhoneNumber":@"strPhoneNumber",@"CompanyName":@"strCompany",@"Address":@"strAddress",@"Abn":@"strAbn"}];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                            method:RKRequestMethodAny
                                                                                       pathPattern:nil
                                                                                           keyPath:nil
                                                                                       statusCodes:[NSIndexSet indexSetWithIndex:200]];

    RKObjectManager *manager = [RKObjectManager sharedManager];
    [manager addRequestDescriptor:requestDescriptor];

    [manager addResponseDescriptor:responseDescriptor];


    RKObjectMapping *errResponseMapping = [RKObjectMapping mappingForClass:[ServiceError class]];

    [errResponseMapping addAttributeMappingsFromDictionary:@{ @"ErrorMessage": @"strErrorMessage", @"FriendlyMessage": @"strFriendlyMessage"}];


    RKResponseDescriptor *errResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errResponseMapping
                                                                                               method:RKRequestMethodAny
                                                                                          pathPattern:nil
                                                                                              keyPath:nil
                                                                                          statusCodes:[NSIndexSet indexSetWithIndex:200]];



    [manager addResponseDescriptor:errResponseDescriptor];

    manager.requestSerializationMIMEType = RKMIMETypeJSON;

    User *user = [user new];
    user.strCode = txtCode.text;


    // POST the parameterized representation of the `page` object to `/posts` and map the response
    [manager postObject:user path:[ServiceUrls userDetails] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
           NSlog(@"%d",result.count);
        }

    } failure:nil];

The user class looks like this

@interface User : NSObject
{

}

@property (nonatomic,retain) NSNumber *Id;
@property (nonatomic,retain) NSString *strCode;
@property (nonatomic,retain) NSString *strFname;
@property (nonatomic,retain) NSString *strLname;
@property (nonatomic,retain) NSString *strEmail;
@property (nonatomic,retain) NSString *strPhoneNum;
@property (nonatomic,retain) NSString *strCompany;
@property (nonatomic,retain) NSString *strAddress;
@property (nonatomic,retain) NSString *strAbn;


@end

JSON response that i get but isn't mapping is as follows

    {"Id":7,
"UserCode":"CC1234",
"FirstName":"Test name_",
"LastName":"Banga",
"Email":"[email protected]",
"PhoneNumber":"0421196587",
"CompanyName":"String",
"Address":"String",
"Abn":"String"}

Not sure whats wrong with the code I have added, any clue ?


Solution

  • Maybe try this. Mapping:

    -(void) getUserMapping {  
        RKEntityMapping *userEntityMapping = [self generateEntityMappingFromClass:[User class]];
        userEntityMapping.identificationAttributes = @[@"Id"];
        return userEntityMapping;
    }
    

    Generate Mapping:

    + (RKEntityMapping *)generateEntityMappingFromClass:(Class)objectClass {
        NSAssert(objectClass != NULL, @"Cannot generate a mapping from a null object class.");
        NSDictionary *propertiesAndClasses = [[RKPropertyInspector sharedInspector] propertyInspectionForClass:objectClass];
        NSAssert([propertiesAndClasses count] > 0, @"Cannot generate a mapping from a class containing zero properties.");
        RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass(objectClass) inManagedObjectStore: [RKManagedObjectStore defaultStore]];
        NSMutableDictionary *mappingDictionary = [[NSMutableDictionary alloc] init];
    
        for (NSString *property in [propertiesAndClasses allKeys]) {
            NSLog(@"property: %@", property);
            [mappingDictionary setObject:property forKey:property];
        }
    
    [mapping addAttributeMappingsFromDictionary:mappingDictionary];
    return mapping;
    }
    

    Response descriptor:

    RKResponseDescriptor *responseDescriptorBody = [RKResponseDescriptor responseDescriptorWithMapping:[self getUserMapping] method:RKRequestMethodGET pathPattern:@"" keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    

    I think your problem is JSON without keyPath, try to change it from nil to @""