我有一個JSON結束路徑接受以下格式的發佈請求。POST與休息套件
{'values': [
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
{
"date":<measurement date as Unix time stamp>
"value":<weight>
}
...]}
「值」由類「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」。
您的類結構真的不清楚,因爲您的映射中有許多不同的類,並且大部分映射看起來都是部分的。你可能會發現創建映射就像你解析JSON然後反過來一樣簡單。否則,您需要顯示完整的課程結構。 – Wain
我用數據結構更新了我的問題。 – ff10