1
我想從以下JSON映射到一個CoreData屬性從元素的集合一個CoreData屬性:映射使用RestKit RKDynamicMapping
{
"products": [
{
"stock": {
"inStock": true,
"inStockBeforeMaxAdvanceOrderingDate": false
},
"url": "xxxxxxxxxxx",
"manufacturer": "Demo",
"images": [
{
"format": "thumbnail",
"imageType": "PRIMARY",
"url": "/imageURL/xx.jpg"
},
{
"format": "thumbnailGrid",
"imageType": "PRIMARY",
"url": "/imageURL/xxx.jpg"
}
],
"productFees": [
[
"EMPTY",
"0.0"
]
],
"name": "demo product"
}
]
}
我想只提取thumbnailGrid圖像集合中的圖像網址。在我的回覆中,我有很多次收集了一個收集,但我只對一個字段感興趣。 我可以在CoreData中建立關係,我沒有問題,但是因爲我知道我只匹配一個資源,我寧願將其存儲在一個屬性中,而不是一個關係。
我的產品CoreData實體具有thumbnailImageURL屬性。
我的產品實體映射:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:_managedObjectStore];
[mapping addAttributeMappingsFromDictionary:@{
@"stock.inStock":@"inStock",
@"stock.inStockBeforeMaxAdvanceOrderingDate":@"inStockBeforeMaxAdvanceOrderingDate",
@"url":@"url",
@"manufacturer": @"manufacturer",
@"manufacturerName":@"manufacturerName",
@"name":@"name",
}];
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"thumbnailImageURL" withMapping:dynamicMapping]];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
if ([[representation objectForKey:@"format"] isEqualToString:@"thumbnailGrid"]) {
return ???;
}
return nil;
}];
我不知道比賽時會發生什麼,從動態映射器返回。我應該創建另一個RKEntityMapping實例,該實例是同一個「產品」實體的映射,並將url映射到thumbnailImageURL?
也可以通過添加某種方式動態映射器作爲屬性而不是關係嗎?
感謝,
喬鮑