RestKit将普通的JSON结构映射到更深层的嵌套对象中

时间:2018-10-08 11:44:36

标签: ios objective-c json restkit

我有以下json:

{
    "id": "abcd",
    "some_value": 123,
    "other_value": 456
}

现在,我想使用RestKit将此JSON映射到如下所示的类结构中:

MyObject
- id: String
- values: MyCustomContainer

MyCustomContainer:
- some_value: MyValue
- other_value: MyValue

MyValue:
- value: Int

所以之后它应该像这样:

{
  "myObject": {
    "id": "abcd",
    "values": {
      "some_value": {
        "value": 123
      },
      "other_value": {
        "value": 456
      }
    }
  }
}

我尝试使用RKObjectMapping来产生中间对象。到目前为止,我能够使用以下方法创建MyValue对象:

RKObjectMapping *someValueMapping = [RKObjectMapping mappingForClass:[MyValue class]];
[someValueMapping addAttributeMappingsFromDictionary:@{@"some_value": @"some_value"}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"some_value" withMapping: someValueMapping]];

这将在MyValue下创建一个MyObject对象,但是我没有成功创建中间容器MyCustomContainer

关于如何实现这一目标的任何想法?

0 个答案:

没有答案