• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 使用 RestKit 的嵌套对象映射

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:14:49 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在尝试将 JSON 映射到对象结构中时遇到了一些困难:

- Group
  + label
  + type
  + children
    - childname1
       + label
       + type
       + value

    - childname2
       + label
       + type
       + value

我确实有一个Group

@interface CameraGroup : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSDictionary *children;
@end

和一个 Child

@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *value;

我的 JSON 如下所示:

{
    "actions": {
        "label": "Action List",
        "type": "section",
        "children": {
            "action1": {
                "label": "This is action1",
                "type": "toggle",
                "value": 0
            },
            "action2": {
                "label": "This is action2",
                "type": "range",
                "value": 0
            },
            "action3": {
                "label": "This is action3",
                "type": "toggle",
                "value": 0
            }
        }
    }
}

我试图以单个组对象结束,使用 RestKit 持有一个带有键值对操作的 NSDictionary(子对象)(例如 action1 = 标签、类型和值的键和子对象)。

我正在使用以下代码段,但总是以一个单一的空子对象结束:

RKObjectMapping *mappingChild = [RKObjectMapping mappingForClass:[Child class]];
  [mappingSetting addAttributeMappingsFromDictionary{
                                                @"label": @"label",
                                                @"type":  @"type",
                                                @"value": @"value"
                                                }];

  RKObjectMapping *mappingGroup = [RKObjectMapping mappingForClass:[Group class]];
  [mappingGroup addAttributeMappingsFromDictionary{
                                                       @"label": @"label",
                                                       @"type":  @"type"
                                                       }];
  [mappingGroup addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath"children" toKeyPath"children" withMapping:mappingSetting]];


  NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingGroup method:RKRequestMethodAny pathPattern"/action" keyPath"actions" statusCodes:statusCodes];

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString"http://192.168.178.1:1337/actions"]];
  RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors[responseDescriptor]];
  [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    Group *group = [result firstObject];
    NSLog(@"Mapped the group: %@ of type %@ with children %i", article.label, article.label, [group.children count]);
  } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
  }];
  [operation start];

那么有什么方法可以在不知道“action1”被命名为“action1”的情况下映射 NSDictionary 部分??



Best Answer-推荐答案


在玩了之后,我找到了一个似乎可行的解决方案。 responseDescriptor获取Group的映射,它有一个关系映射到一个dynamicMapping,它遍历所有子节点并映射childMapping

RKObjectMapping *mappingChild = [RKObjectMapping mappingForClass:[Child class]];

  [mappingSetting addAttributeMappingsFromDictionary{
                                                       @"label" : @"label",
                                                       @"type" : @"type",
                                                       @"value" : @"value"
                                                       }];

  RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
  [dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

    NSArray *keys = [representation allKeys];

    RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

    for (NSString *key in keys) {
      [dataMapping addRelationshipMappingWithSourceKeyPath:key mapping:mappingChild];
    }

    return dataMapping;

  }];

  RKObjectMapping *mappingGroup = [RKObjectMapping mappingForClass:[Group class]];
  [Group addAttributeMappingsFromDictionary{
                                                     @"label": @"label",
                                                     @"type":  @"type"
                                                     }];

  [Group addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:dynamicMapping]];







  NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingGroup
                                                                                          method:RKRequestMethodAny
                                                                                     pathPattern:@"/actions"
                                                                                         keyPath:@"actions"
                                                                                     statusCodes:statusCodes];

关于ios - 使用 RestKit 的嵌套对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21504273/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap