RestKit - 数据映射

时间:2015-10-30 10:37:51

标签: objective-c mapping restkit data-mapping

我是RestKit的新手。  有人可以帮我映射。我有JSON,我需要从JSON读取和保存数据。我怎么能这样做?

 {"data": {
    "viewer": {
      "themes": {
        "edges": [
          {
            "node": {
              "id": "61c39",
              "name": "ff"
            }
          }
        {
            "node": {
              "id": "dd95af4b-"",
              "name": "growth",
            }
          }
        ]
      }
    }
  }
}

以下是我的映射代码的一部分:

// Defines mapping for DefaultThemes
RKEntityMapping *mappingDefaultTheme = [RKEntityMapping mappingForEntityForName:@"DefaultThemeData"
                                                         inManagedObjectStore:self.managedObjectStore];
mappingDefaultTheme.identificationAttributes = @[@"themeId"];
[mappingDefaultTheme addAttributeMappingsFromDictionary:@{ @"node.id" : @"themeId", @"node.name" : @"name"}];



RKResponseDescriptor *themeDefaultListResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingDefaultTheme
                                                                                                   method:RKRequestMethodGET
                                                                                              pathPattern:@"graphql"
                                                                                                  keyPath:@"edges.node"
                                                                                              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

运行应用时出错:

Error: No mappable object representations were found at the key paths searched.

所以我需要知道,我应该如何进行数据映射?

2 个答案:

答案 0 :(得分:0)

关键路径data.viwer.themes.edges.node太深,因为您尝试深入研究数组并且这是不可能的。您可以拥有的最长密钥路径为data.viwer.themes.edges,然后映射将处理包含“节点”的每个对象。

答案 1 :(得分:0)

这是我的解决方案:

RKObjectMapping* chartDataDetailMapping = [RKObjectMapping mappingForClass:[MADefaultThemeData class]];

[chartDataDetailMapping addAttributeMappingsFromDictionary:@{@"id": @"themeId",
                                                             @"name": @"name"}];

RKObjectMapping* chartDataMapping = [RKObjectMapping mappingForClass:[MANode class]];
[chartDataMapping addAttributeMappingsFromDictionary:@{@"edges":@"edges"}];

RKObjectMapping* alertInstanceMapping = [RKObjectMapping mappingForClass:[MADefaultThemeList class]];

[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"edges"
                                                                                     toKeyPath:@"edges"
                                                                                   withMapping:chartDataMapping]];

[chartDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"node"
                                                                                     toKeyPath:@"themeData"
                                                                                   withMapping:chartDataDetailMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:alertInstanceMapping
                                                                                                        method:RKRequestMethodPOST
                                                                                                        pathPattern:@"graphql"
                                                                                                        keyPath:@"data.viewer.themes"
                                                                                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[self addResponseDescriptor:responseDescriptor];