【问题标题】:Specify a specific mapping for objects in JSON array in RESTkit?为 RESTkit 中 JSON 数组中的对象指定特定映射?
【发布时间】:2012-03-05 19:26:43
【问题描述】:

当我请求 UserProfile 对象列表时,我从 django-rest-framework 收到此 JSON:

[
    {
        "gender": 1, 
        "show_gender": true, 
        "show_real_name": true
    }, 
    {
        "gender": 2, 
        "show_gender": true, 
        "show_real_name": true
    }
]

但是我不知道如何使用 Restkit 配置我的映射。 Restkit 似乎期待dictionary instead of a list,因为它似乎使用字典的键作为“KeyPath”来识别必要的映射。有没有办法手动指定接收到的对象的映射?

【问题讨论】:

    标签: ios json restkit


    【解决方案1】:

    知道了!

    // define a mapping
    RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[ERUser class]];
    [userMapping mapAttributes:
     @"gender",
     @"show_gender",
     @"show_real_name",
     nil];
    
    // add the mapping anonymously
    [objectManager.mappingProvider addObjectMapping:userMapping];
    
    // tell the mapping provider which mapping to use
    RKObjectMapping* articleMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[ERUser class]];
    [objectManager loadObjectsAtResourcePath:@"/users" objectMapping:articleMapping delegate:self];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-15
      • 2016-03-27
      • 2012-05-28
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      相关资源
      最近更新 更多