RestKit - 数据映射

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.

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

关键路径 data.viwer.themes.edges.node 太深,因为您正试图向下钻取数组,但这是不可能的。您可以拥有的最长键路径是 data.viwer.themes.edges,然后映射将处理包含 'nodes'.

的每个对象

这是我的解决方案:

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)];

[自我 addResponseDescriptor:responseDescriptor];