【问题标题】:RestKit mapping with NSDictionary and NSArray objectRestKit 映射与 NSDictionary 和 NSArray 对象
【发布时间】:2017-02-10 01:01:48
【问题描述】:
 {"coord":{"lon":72.62,"lat":23.03},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":303.082,"pressure":1014.85,"humidity":66,"temp_min":303.082,"temp_max":303.082,"sea_level":1018.46,"grnd_level":1014.85},"wind":{"speed":1.07,"deg":340.501},"rain":{"3h":0.435},"clouds":{"all":76},"dt":1475333911,"sys":{"message":0.0033,"country":"IN","sunrise":1475283682,"sunset":1475326567},"id":1279233,"name":"Ahmadabad","cod":200}

以上是我的 API 响应。

现在我想映射“天气”和“名称”,并希望将相同的对象作为响应。

我可以创建类

@interface WeatherStatus : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) WeatherInfo *info;
@end

@interface WeatherInfo : NSObject
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *icon;

下面是映射代码。

 RKObjectMapping *weatherInfo = [RKObjectMapping mappingForClass:[WeatherInfo class]];
[weatherInfo addAttributeMappingsFromDictionary:@{@"description": @"description", @"icon": @"icon"}];

RKObjectMapping *weatherStatus = [RKObjectMapping mappingForClass:[WeatherStatus class]];

[weatherStatus addAttributeMappingsFromArray:@[@"name"]];
[weatherStatus addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"weather" toKeyPath:@"weather" withMapping:weatherInfo]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:weatherStatus method:RKRequestMethodGET pathPattern:nil keyPath:@"weather" statusCodes:nil];

    [objectManager addResponseDescriptor:responseDescriptor];

    NSDictionary *queryParams;
    queryParams = [NSDictionary dictionaryWithObjectsAndKeys:kAPP_KEY, @"appid", @"Ahmedabad", @"q", nil];
    [[RKObjectManager sharedManager] getObjectsAtPath:@"/data/2.5/weather" parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        WeatherStatus *obj = [mappingResult.array objectAtIndex:0];
        NSLog(@"info %@",obj.info);
        NSLog(@"name %@",obj.name);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"What do you mean by 'there is no coffee?': %@", error);
    }];

我来了

info (null)
name (null)

谁能告诉我错误在哪里?

我已经看过RestKit complex and simple JSON RKObjectMapping almost working, but

【问题讨论】:

    标签: ios objective-c json restkit restkit-0.20


    【解决方案1】:

    不要使用description 作为属性名,它只会给你带来麻烦。请改用overview 或类似的东西。

    在 JSON 中,天气是一个数组,因此您应该将天气(信息)属性设为 NSArray,并确保映射中的名称与属性匹配。

    【讨论】:

    • 我有点困惑。在stackoverflow.com/questions/23529494/…“位置”是数组格式!
    • 这是一个顶级数组,不是嵌套的。这是一个关键的区别。
    • 这不是我说要做的。您的 info 属性与映射不匹配,需要为数组
    • 我刚刚明白了!谢谢:)
    【解决方案2】:

    我将“WeatherStatus”类中 info 对象的属性更改为 NSArray。

    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, strong) NSArray *weather;
    

    这里是映射代码的修改。

    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[WeatherStatus class]];
    [venueMapping addAttributeMappingsFromDictionary:@{@"name": @"name"}];
    
    RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[WeatherInfo class]];
    [locationMapping addAttributeMappingsFromDictionary:@{@"description": @"description",@"icon":@"icon"}];
    
    [venueMapping addRelationshipMappingWithSourceKeyPath:@"weather" mapping:locationMapping];
    
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:nil];
    
    [objectManager addResponseDescriptor:responseDescriptor];
    

    添加与 WeatherStatus 类的关系映射。

    归功于https://stackoverflow.com/users/1988185/wain

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多