我在映射JSON 数据中的latest_update字段时遇到问题。
从我的网络服务接收这个 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" } ... 到 place 对象中,但是latest_update没有映射到 LatestDBUpdate 类对象,我不能以任何方式让它工作。
我希望有人知道它是如何完成的,因为数小时的搜索和尝试让我离解决方案更近了一步。感谢托马斯