我遇到了: latest_update 字段的映射问题 JSON数据。
从我的网络服务中获取此JSON数据:
{"Places":[
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a",
"timestamp": "2011-10-02 23:24:42"
},
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x",
"timestamp": "2011-10-02 23:24:42"
},
{"latest_update":"2011-10-13 12:16:17"}]
}
我用于托管映射的代码片段:
//Place mapping
if (!self.placeManagedObject){
self.placeManagedObject = [RKManagedObjectMapping
mappingForClass:[Place class]];
self.placeManagedObject.primaryKeyAttribute = @"UUID";
self.placeManagedObject.rootKeyPath = @"places";
[self.placeManagedObject mapKeyPath:@"place_ID"
toAttribute:@"UUID"];
[self.placeManagedObject mapKeyPath:@"timestamp"
toAttribute:@"timestamp"];
[self.placeManagedObject mapRelationship:@"PlaceInformation"
withMapping:self.placeInfoManagedObject];
// Register mapping with the provider - means it will look for
places in the JSON input
[objectManager.mappingProvider
setMapping:self.placeManagedObject forKeyPath:@"places"];
}
//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
self.latestDBUpdateManagedObject = [RKManagedObjectMapping
mappingForClass:[LatestDBUpdate class]];
self.latestDBUpdateManagedObject.primaryKeyAttribute =
@"latest_update";
self.latestDBUpdateManagedObject.rootKeyPath = @"places";
[self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"
toAttribute:@"latest_update"];
// Register mapping with the provider - means it will look for
places in the JSON input
[objectManager.mappingProvider
setMapping:self.latestDBUpdateManagedObject
forKeyPath:@"latest_update"];
}
RestKit将Place对象映射正确,即: {“place_ID”:“7cceedda-ed3a-11e0-a1a8-858e3974979a”, “timestamp”:“2011-10-02 23:24:42” } ... 放入物体, 但 latest_update 未映射到 LatestDBUpdate类对象,我不能以任何方式让它工作。
我希望有人能够解决它是如何完成的,因为数小时 搜索和尝试使我没有更接近解决方案。 Thanx Thomas
答案 0 :(得分:2)
所以我终于明白了。 基本上是一些小的调整,代表我的几个错误: - /
所以我将我的webservice的JSON输出更改为这种格式:
{"Places":[
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a",
"timestamp": "2011-10-02 23:24:42"
},
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x",
"timestamp": "2011-10-02 23:24:42"
}],
"Timestamps":[
{"latest_update":"2011-10-13 12:16:17"}]
我在映射og时间戳中出错 - > latest_update,所以我修复了:
//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
self.latestDBUpdateManagedObject.rootKeyPath = @"Timestamps";
[self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" toAttribute:@"latest_update"];
// Register mapping with the provider - means it will look for the keyword Timestamps in the JSON input
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];
然后我的get方法出现了重大错误。基本上我告诉RestKit,响应应该只通过我的Place贴图映射,因此不会检查JSON输入中的所有其他数据是否包含其他关键字。 只是为了显示差异,我提出了我使用的两种GET方法。第一个将仅使用指定的映射方案进行映射,第二个将使用所有注册的映射方案进行检查:
GET方法编号1 :(在上述情况下不起作用!)
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self];
GET方法数字2 :(这里检查所有映射方案,并映射Place对象,以及我的LatestDBUpdate对象!)
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self];
希望有一天有人可能需要这个:-D
托马斯