0

我从 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"
},

我有课程作为流动场所课程:

@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 照片 Alawys 的值为零。

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

4

1 回答 1

1

keyPaths 是您缺少的部分。当你说 Photos 总是 nil 时,我猜你并没有真正得到任何有意义的数据?要获取您需要在responseDescriptor.

以类似的方式,您可以html_attributions通过将其用作另一组映射和响应描述符的 keyPath 来获取 。

于 2013-05-28T08:10:04.030 回答