2015-01-21 88 views
0

我已经从REST服务的JSON响应我使用RESTKit和其没有得到映射的,下面是对于相同的RESTful服务响应不映射到一个对象 - RESTKit

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]; 

用户类的源看起来是这样的

@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响应,我得到的却是不映射如下

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

不知道我添加的代码有什么问题,任何线索?

+0

你有什么错误吗?对象是空的吗?你有没有试图排除领域,以至少有一个工作? – Miknash 2015-01-21 11:31:38

+0

@NickCatib没有错误,只是说没有找到任何可映射的对象,我没有尝试删除任何字段,如 – 2015-01-21 11:51:51

+0

@NickCatib即使只有一个字段,它仍然工作仍然会得到空的结果对象 – 2015-01-21 11:55:01

回答

2

也许试试这个。 映射:

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

生成贴图:

+ (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; 
} 

响应描述:

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

我觉得你的问题是JSON没有的keyPath,试图将其从零到@变 “”

+0

,这是一声巨响,将关键路径设置为@“ “帮助感谢:D – 2015-01-21 12:46:21