2011-10-17 121 views
0

我有一个问题:latest_update字段在 JSON数据的映射。RestKit - “高级数据对象”的对象映射问题?

从我的web Recieving这个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_ID”:“7cceedda-ed3a-11e0-a1a8-858e3974979a”, “timestamp”:“2011-10-02 23:24:42” } ... 到位对象, 但latest_update没有被映射到 LatestDBUpdate类对象,我不能以任何方式得到它的工作。

我希望有人可以回答它是如何完成的,因为搜寻和尝试的小时数已经让我没有接近解决方案。 Thanx Thomas

回答

2

所以我终于明白了。 基本上一些小的调整,并代表我的一对夫妇的错误: -/

所以我改变了JSON输出从我的web这个格式:

{"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,所以我fixxed:

//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方法。第一个将只与指定的映射方案映射和二号将与所有registred映射方案检查:

GET方法1号:(!这并不在上述背景下工作)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self]; 

GET方法NUMER 2:(这里所有的映射方案进行检查,并都将对象映射,以及我LatestDBUpdate对象!)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self]; 

希望有人可能需要这一天:-D

Thomas