我有一个嵌套的 json 结构如下:
{
"receiver-id":123456,
"rewards": [
{
"content": "reward text 1",
"reward-code": "456ABC",
"reward-Id": "234"
},
{
"content": "reward text 2",
"reward-code": "012ABC",
"reward-Id": "567"
}
]
}
我有一个实体对象如下,我希望将上面的 json 直接映射到:
@interface Reward: NSManagedObject
@property (nonatomic, retain) NSNumber * receiverID;
@property (nonatomic, retain) NSString * content;
@property (nonatomic, retain) NSString * rewardCode;
@property (nonatomic, retain) NSString * rewardID;
如何将上述 json 结构直接映射到上述Reward
实体?我不想拥有一个集合或另一个实体来存储rewards
来自 json 的内容。我只想将每个集合与相应的高级属性一起rewards
存储为实体中的单独行。Reward
receiverID
我尝试了以下映射,但只有receiverID
正在映射。我究竟做错了什么?在这种情况下如何访问rewards
数组元素?
_rewardsMapping = [RKEntityMapping mappingForEntityForName:@"Reward" inManagedObjectStore:objectStore];
_rewardsMapping.forceCollectionMapping = YES;
_rewardsMapping.identificationAttributes = @[ @"rewardID" ];
[_rewardsMapping addAttributeMappingsFromDictionary:@{
@"receiver-id":@"receiverID",
@"(rewards).content": @"content",
@"(rewards).reward-code": @"rewardCode",
@"(rewards).reward-id": @"rewardID"
}];