RestKit与NSDictionary和NSArray对象的映射 [英] RestKit mapping with NSDictionary and NSArray object

查看:110
本文介绍了RestKit与NSDictionary和NSArray对象的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 {"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响应.

Above is my API response.

现在,我想映射天气"和名称",并希望使用相同的对象作为响应.

Now I want to mapping of "weather" and "name" and want the same object as a response.

我可以创建课程

@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)

任何人都可以让我知道哪里出了错吗?

Can anyone let me know where is the mistake?

我已经看过 RestKit复杂而简单的JSON RKObjectMapping几乎工作,但是

推荐答案

我将"WeatherStatus"类中的info对象的属性更改为NSArray.

I change the property of info object in "WeatherStatus" class to 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类添加关系映射.

Add relationship mapping with WeatherStatus class.

信用转到 https://stackoverflow.com/users/1988185/wain

这篇关于RestKit与NSDictionary和NSArray对象的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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