【发布时间】:2018-08-17 13:33:09
【问题描述】:
我的服务器有以下 JSON...
{"Devices": [
{
"uuid": "d9084134-d5f7-43a3-9613-7faa769a822a",
"label": "",
"location": "a13d45f4-5ce0-48e3-bc3f-4076bb007037",
"capability": 0
},
{
"uuid": "a4ee0d3f-4a6a-4c61-81bd-3dfa9ab19e85",
"label": "",
"location": "a13d45f4-5ce0-48e3-bc3f-4076bb007037",
"capability": 3
}
]}
我正在尝试正确映射它,但我正在努力解决它......我有以下映射设置......
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Device" inManagedObjectStore:managedObjectStore];
[entityMapping setIdentificationAttributes:[NSArray arrayWithObject:@"uuid"]];
[entityMapping addAttributeMappingsFromDictionary:@{
@"uuid": @"uuid",
@"label": @"label",
@"location": @"location",
@"capability": @"capability"
}];
// GET
RKRequestDescriptor *getRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[entityMapping inverseMapping] objectClass:[Device class] rootKeyPath:@"Device" method:RKRequestMethodGET];
[[RKObjectManager sharedManager] addRequestDescriptor:getRequestDescriptor];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Device class] pathPattern:@"Devices" method:RKRequestMethodGET]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"Devices" keyPath:@"Devices" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
我将我的 keyPath 设置为我认为可行的“设备”。但是 RestKit 不理解它。
我正在使用以下内容从我的服务器获取对象...
[[RKObjectManager sharedManager] getObjectsAtPath:@"Devices" parameters:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d", clientID], @"clientId", @"topic", @"forTopic", nil] success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
...
}... // Abbreviated for brevity.
所以我相信我的路径“设备”对于“设备”的 pathPattern 是正确的。参数搞砸了吗?我之前有过参数,它总是可以工作,而无需在映射中指定任何内容。
我目前使用的是 RestKit 0.27.1。
更新 - 修复 JSON
我修复了 Mahendra GP 提到的 JSON。所以这现在是一个合适的数组。 RestKit 日志现在显示跟踪日志中的 JSON。但是它仍然无法识别它是哪个对象(设备)。
日志片段...
...and targetObject: (null)
2018-03-09 02:44:05.603734-0700 MyTestApp[31576:6868006] D restkit.object_mapping:RKMapperOperation.m:440 Finished performing object mapping. Results: (null)
2018-03-09 02:44:05.605334-0700 MyTestApp[31576:6868006] E restkit.network:RKObjectRequestOperation.m:172 GET 'http://[MY_IP]:8080/Devices?clientId=5199&forTopic=topic' (200 OK / 0 objects) [request=2.6488s mapping=0.0000s total=2.6633s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded."
【问题讨论】:
-
检查您的 json 数据,它在“设备”下有重复的键“设备”...我们不允许!
标签: ios objective-c restkit restkit-0.20