RKObjectMapping 中的混淆设置映射 - Rest
Posted
技术标签:
【中文标题】RKObjectMapping 中的混淆设置映射 - Rest【英文标题】:Confusion setting mapping in RKObjectMapping - Rest 【发布时间】:2013-09-19 08:47:55 【问题描述】:请帮助使用 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 个属性作为成功/消息以及所有其他属性,但我坚持设置自定义映射,因为这些属性位于根目录上,而其他属性位于关键路径“result.data”中。
有人可以帮忙吗?
【问题讨论】:
【参考方案1】:您应该将您的登录映射更改为:
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)];
基本上,从响应描述符的键路径中删除“数据”,然后在映射键路径中使用它。
【讨论】:
【参考方案2】:您可以先通过检查以下条件来检查数据是否存在
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");
希望这会对您有所帮助。
【讨论】:
映射是使用restkit完成的,这仍然适用吗? 你一定在 RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor respon..... 这行仪式上遇到错误?所以把你的整个代码放在 if 条件中,如果数据不可用,则在 else 部分处理这种情况.. 我已经更新了如何调用,它总是进入失败块并出现我说的错误。 感谢您的帮助,但无济于事。以上是关于RKObjectMapping 中的混淆设置映射 - Rest的主要内容,如果未能解决你的问题,请参考以下文章
如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?
使用 RKParams 向 API 发布帖子并使用 RestKit 使用 RKObjectMapping 映射响应