0

我在映射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 类对象,我不能以任何方式让它工作。

我希望有人知道它是如何完成的,因为数小时的搜索和尝试让我离解决方案更近了一步。感谢托马斯

4

1 回答 1

2

所以我终于想通了。基本上是一些小的调整,以及代表我的几个错误:-/

因此,我将 Web 服务的 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 Timestamps -> 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

托马斯

于 2011-10-18T13:45:10.467 回答