在restkit v.20中映射数组

时间:2013-05-28 07:21:47

标签: ios objective-c restkit restkit-0.20

我从Google Places API中读取

{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.870540,
"lng" : 151.1988150
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
"id" : "c71365287e7606bd21a3311d21fda087830b7813",
"name" : "Pancakes on the Rocks",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 1224,
"html_attributions" : [
"\u003ca href=\"https://plus.google.com/105663944571530352563\"\u003eJoshua Gilmore\u003c/a\u003e"
],
"photo_reference" : "CnRoAAAADF1JoE-joT2BSN4NEYn2luUF2kdIRjflFvETx0SbCnPyRp0TZz8_x7OlHCpAz2rrNXtQZyp5wd-JEjZh2FSpkM2bDGs_DqmktsClrnYkkirkCOHlnBHxedJf_Etv0DV8y9vnIbFnLJ9995Fk2u98SBIQxjojXwmV45p-9b4SurLQhxoUtErGJpwFe-k0hfgVGapK1FYjnWw",
"width" : 1632
}
],
"price_level" : 2,
"rating" : 3.90,
"reference" : "CoQBcgAAAFakUaktwUsNCr--KIaYu_hS9cbO8uMAwNE2W7xSMEZimIjc6EhMStq2LpVteXf6jy4UqeHGJh0QhDSpKMGK065jlnha2F6bG1zoLDRHtYTqV3PIOMMa8KsjJgmLUU-7GDojLvb6MHVctzMbMeTGtAOq_mm5lS_oymzDtJJsehSkEhCZ-KMMyl3qmYNHkCJ919FRGhSlbP1fVPQ_mLamjx9ELIBulb4ACg",
"types" : [ "cafe", "restaurant", "food", "establishment" ],
"vicinity" : "Harbourside Shopping Centre,Darling Harbour/227 & 229-230 Darling Drive, Sydney"
},

我有Classes As Flow 地点类:

@interface Place : NSObject
@property (nonatomic,strong) Geometry * geometry;
@property (nonatomic,strong) NSString * icon;
@property (nonatomic,strong) NSString * placeID;
@property (nonatomic,strong) NSString * name;
@property (nonatomic,strong) OpeningHours * opening_hours;
@property (nonatomic,strong) NSString * price_level;
@property (nonatomic,strong) NSString * rating;
@property (nonatomic,strong) NSString * vicinity;
@property (nonatomic,strong) NSSet *photos;
@property (nonatomic,strong) NSString *reference;
@end

和照片类:

@interface Photos : NSObject
@property (nonatomic,strong) NSNumber *height;
@property (nonatomic,strong) NSString *html_attributions;
@property (nonatomic,strong) NSString *photo_reference;
@property (nonatomic,strong) NSNumber *width;
@end

和我用Follwoing映射:

RKObjectMapping *placeMapping = [RKObjectMapping mappingForClass:[Place class]];
[placeMapping addAttributeMappingsFromDictionary:@{
@"icon" : @"icon",
@"id" : @"placeID",
@"name" : @"name",
@"reference" : @"reference",
@"price_level" : @"price_level",
@"rating" : @"rating",
@"vicinity" : @"vicinity",
}];

//创建我们的新作者映射

RKObjectMapping* geomtryMapping = [RKObjectMapping mappingForClass:[Geometry class]];

RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[Location class]];

[locationMapping addAttributeMappingsFromArray:@[ @"lat", @"lng" ]];

[geomtryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"location"
                                                                              toKeyPath:@"location"
                                                                            withMapping:locationMapping]];

[placeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"geometry"
                                                                             toKeyPath:@"geometry"
                                                                           withMapping:geomtryMapping]];


RKObjectMapping* openingHoursMapping = [RKObjectMapping mappingForClass:[OpeningHours class]];
[openingHoursMapping addAttributeMappingsFromArray:@[@"open_now"]];
[placeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"opening_hours" toKeyPath:@"opening_hours" withMapping:openingHoursMapping]];

RKObjectMapping* photosMapping = [RKObjectMapping mappingForClass:[Photos class]];

[photosMapping addAttributeMappingsFromArray:@[@"height",@"photo_reference",@"html_attributions",@"width"]];
[photosMapping setForceCollectionMapping:YES];
[placeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"photos"
toKeyPath:@"photos"
withMapping:photosMapping]];

RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:placeMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:@""
                                                                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

第一个问题 I Photos Alawys的价值为Nil。

第二个问题我如何在json的开头获得“html_attributions”。

1 个答案:

答案 0 :(得分:1)

keyPaths是你缺少的。当你说照片总是零时,我想你真的没有得到任何有意义的数据?要获取您需要在responseDescriptor上指定@“结果”的keyPath的位置。

以类似的方式,您可以使用它作为另一组映射和响应描述符上的keyPath来获取html_attributions