簡體   English   中英

RestKit與NSDictionary和NSArray對象的映射

[英]RestKit mapping with NSDictionary and NSArray object

 {"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復雜而簡單的JSON RKObjectMapping幾乎可以工作,但是

不要將description用作屬性名稱,它只會給您帶來麻煩。 請使用overview或類似內容。

在JSON中,weather是一個數組,因此您應將weather(info)屬性設為NSArray ,並確保映射中的名稱與該屬性匹配。

我將“ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM