0

我是 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.

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

4

2 回答 2

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

[自我 addResponseDescriptor:responseDescriptor];

于 2015-11-05T09:38:46.827 回答
0

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

于 2015-10-30T15:02:36.333 回答