RestKit:映射复杂嵌套的问题,需要forceCollectionMapping的多个级别 [英] RestKit: Problems mapping complex nesting, multiple levels needing forceCollectionMapping

查看:84
本文介绍了RestKit:映射复杂嵌套的问题,需要forceCollectionMapping的多个级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RestKit映射一些JSON数据,但到目前为止无法正确完成. 数据是日期和注册的分层集合.看起来如下(我无法更改它):

I'm trying to map some JSON data with RestKit, and have so far been unable to do it right. The data is a hierarchical set of days and registrations. It looks like the following (not a possibility for me to change it):

{
  "days": 
  {
    "2012-10-29": {
        "2106303": {
            "activityCode": "LU",
            "approved": false,
            "hours": 0.5,
            "description": "some description",
        },
        "2106304": {
            "activityCode": "VF",
            "approved": false,
            "hours": 7.5,
            "description": "some other description",
        }
        },
        "2012-10-30": null,
  }
}

我的代码如下:

- (void)setupWeekMappingForManager:(RKObjectManager *)manager
{
    RKObjectMapping *registrationMapping = [RKObjectMapping mappingForClass:[Registration class]];
    registrationMapping.forceCollectionMapping = YES;
    [registrationMapping mapKeyOfNestedDictionaryToAttribute:@"registrationNumber"];

    RKObjectMapping *dayMapping = [RKObjectMapping mappingForClass:[Day class]];
    dayMapping.forceCollectionMapping = YES;
    [dayMapping mapKeyOfNestedDictionaryToAttribute:@"date"];

    // how can this relationship be mapped?
    [dayMapping mapKeyPath:@"(date)." toRelationship:@"registrations" withMapping:registrationMapping]; 

    [manager.mappingProvider setMapping:dayMapping forKeyPath:@"days"];
}

很高兴返回日期,但是我找不到映射两种类型之间关系的好方法,

It happily returns the days, but I am unable to find a good way to map the relationship between the two types,

任何朝正确方向的推动将不胜感激,

Any push in the right direction would be greatly appreciated,

最好的问候, 汤米

推荐答案

这对我有用:

- (void)setupWeekMappingForManager:(RKObjectManager *)manager
{
    RKObjectMapping *registrationMapping = [RKObjectMapping mappingForClass:[Registration class]];
    registrationMapping.forceCollectionMapping = YES;
    [registrationMapping mapKeyOfNestedDictionaryToAttribute:@"registrationNumber"];
    [registrationMapping mapKeyPath:@"(registrationNumber).description" toAttribute:@"description"];

    RKObjectMapping *dayMapping = [RKObjectMapping mappingForClass:[Day class]];
    dayMapping.forceCollectionMapping = YES;
    [dayMapping mapKeyOfNestedDictionaryToAttribute:@"date"];
    [dayMapping mapKeyPath:@"(date)" toRelationship:@"registrations" withMapping:registrationMapping];

    [manager.mappingProvider setMapping:dayMapping forKeyPath:@"days"];
}

这给了我一个日对象,该对象具有很好的小时注册数组,作为NSArray. 供参考,它看起来像这样:

This gives me a day object with a nice array of hour registrations as an NSArray. For reference, it looks like this:

@interface Day : NSObject
@property (nonatomic, readonly, copy) NSDate *date;
@property (nonatomic, readonly, strong) NSMutableArray *registrations;
@end

这篇关于RestKit:映射复杂嵌套的问题,需要forceCollectionMapping的多个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆