RKObjectMapping中的混淆设置映射 - Rest

时间:2013-09-19 08:47:56

标签: iphone ios restkit-0.20 rkobjectmapping

请帮助使用RestKit设置映射,我很想知道如何执行此操作。

我的JSON就像

{
"result":{
    "success":"1",
    "message":"You logged in successfully.",
    "data":{
        "user_id":"1",
        "firstname":"somefirstname",
        "lastname":"somelastname",
        "username":"someusername",
        "country_id":"someid",
        "country_name":"somecountry",
        "phone":"1234567890",
        "status":"active"
        }
    }
}

到目前为止,我所做的是

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"user_id":@"intUserID", @"firstname":@"strFirstName", @"lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"PathTOLogin"
                                                                                          keyPath:@"result.data"
                                                                                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURL *baseURL = [NSURL URLWithString:kAPIBaseURLString];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
Login *loginObject = [[Login alloc] init];
[objectManager getObject:loginObject path:kUserLogin parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

问题就像有时JSON响应可能像

一样
{
"result":{
    "success":"0",
    "message":"Invalid Username or Password",      
    } 
}

所以它会抛出错误

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001

所以我想在Model类中添加另外2个属性作为success / message以及所有其他属性,但我坚持设置自定义映射,因为这些属性在root上,而其他属性在关键路径“result.data”中。

有人可以帮忙吗。

2 个答案:

答案 0 :(得分:2)

您应该将登录映射更改为:

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"success":@"success", @"message":@"message", @"data.user_id":@"intUserID", @"data.firstname":@"strFirstName", @"data.lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@"PathTOLogin"
                                                                                       keyPath:@"result"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

基本上,从响应描述符的键路径中删除“data”,而不是在映射键路径中使用它。

答案 1 :(得分:0)

您可以通过检查以下条件来检查数据是否存在

if([yourDictionary valueForKeyPath:@"result.data"]){

   RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"user_id":@"intUserID", @"firstname":@"strFirstName", @"lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"PathTOLogin"
                                                                                          keyPath:@"result.data"
                                                                                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURL *baseURL = [NSURL URLWithString:kAPIBaseURLString];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
Login *loginObject = [[Login alloc] init];
[objectManager getObject:loginObject path:kUserLogin parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

}
else{
    NSLog(@"No data available");
}

希望这会对您有所帮助。