2016-10-01 1 views
0
{"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} 

Ci-dessus, ma réponse API.Mappage RestKit avec objet NSDictionary et NSArray

Maintenant je veux cartographier "météo" et "nom" et je veux le même objet en guise de réponse.

Je peux créer à la classe

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

et

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

ci-dessous est le code de la cartographie.

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); 
    }]; 

Je reçois

info (null) 
name (null) 

Quelqu'un peut-il me faire savoir où est l'erreur?

Je l'ai déjà vu RestKit complex and simple JSON RKObjectMapping almost working, but

Répondre

0

Je change la propriété de l'objet info dans la classe "WeatherStatus" en NSArray.

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

Voici la modification du code de mappage.

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]; 

Ajouter un mappage de relation avec la classe WeatherStatus. Le crédit passe à https://stackoverflow.com/users/1988185/wain

1

Ne pas utiliser description comme nom de la propriété, cela ne fera que vous causer des problèmes. Utilisez overview ou quelque chose de similaire à la place.

Dans le JSON, la météo est un tableau. Vous devez donc définir votre propriété météo (info) comme NSArray et assurez-vous que le nom du mappage et de la propriété correspondent.

+0

Je suis confus. http://stackoverflow.com/questions/23529494/restkit-complex-and-simple-json-rkobjectmapping-almost-working-but "emplacement" est dans un format de tableau! – kb920

+0

C'est un tableau de niveau supérieur, non imbriqué. C'est une différence clé. – Wain

+0

J'ai essayé la description comme tableau mais cela n'a pas fonctionné! – kb920