我有一个Web服务,它以下列格式返回JSON:
{
"client": {
"firstName": "Aaron"
},
"error": {
"code":""
}
}
我正在设置restkit中的映射。我一直收到以下错误:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Could not find an object mapping for keyPath: ''" UserInfo=0x5961f0 {=RKObjectMapperKeyPath, NSLocalizedDescription=Could not find an object mapping for keyPath: ''}
以下是我设置映射的方法:
[[RKObjectManager sharedManager].mappingProvider setMapping:[Client objectMapping] forKeyPath:@"client"];
[[RKObjectManager sharedManager].mappingProvider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];
客户端映射:
+(RKObjectMapping*) objectMapping {
RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[Client class]];
[responseObjectMapping setRootKeyPath:@"client"];
[responseObjectMapping mapKeyPathsToAttributes:
@"firstName", @"firstName",
nil];
return responseObjectMapping;
}
错误映射:
+(RKObjectMapping*) objectMapping {
RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[ErrorObject class]];
[responseObjectMapping setRootKeyPath:@"error"];
[responseObjectMapping mapKeyPathsToAttributes:
@"code", @"code",
nil];
return responseObjectMapping;
}
并按照以下方式调用服务:
//Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
loader.userData = INITIAL_LOAD_REQUEST;
}];
修改
我通过遵循以下建议解决了我的问题,并将我的映射从init方法移动到在对象加载器块中设置它,如下所示:
//Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
//set mapping explicitly
RKObjectMappingProvider *provider = [[RKObjectMappingProvider alloc] init];
[provider setMapping:[Clien objectMapping] forKeyPath:@"client"];
[provider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];
[loader setMappingProvider:provider];
loader.userData = INITIAL_LOAD_REQUEST;
}];
答案 0 :(得分:2)
删除行
[responseObjectMapping setRootKeyPath:@"client"];
和
[responseObjectMapping setRootKeyPath:@"error"];