我有一个 JSON 结束路径,它接受以下格式的发布请求。
{'values': [
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
...]}
“Values”由“EntryCollection”类表示,而每个值由“Entry”类表示。我很困惑找到将我的对象映射到 JSON 表示的正确方法。现在我有以下导致错误的代码:“映射操作无法在搜索的关键路径中找到任何嵌套对象表示”。
RKObjectMapping *entryMapping = [RKObjectMapping requestMapping];
RKObjectMapping *valuesMapping = [RKObjectMapping mappingForClass:[EntriesCollection class]];
[valuesMapping addAttributeMappingsFromDictionary:[EntryCollection attributesMapping]];
[singleEntryMapping addAttributeMappingsFromDictionary:[SingleEntry attributesMapping]];
[singleEntryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"entries" toKeyPath:@"entries" withMapping:valuesMapping]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:entryMapping
objectClass:mappedClass
rootKeyPath:nil];
[self.objectManager addRequestDescriptor:requestDescriptor];
NSString *path = [self pathForPOST];
[self.objectManager postObject:weights path:path parameters:nil success:nil failure:nil];
编辑数据结构
我的数据结构很简单(我想):
EntryCollection
- NSArray *entries (a collection of objects of type Entry)
Entry
- NSDate *date
- NSNumber *weight;
我想发布一个充满条目的 EntryCollection。EntryCollection 的映射是“entries -> values”,Entry 的映射是“date -> date, weight -> value”。