RestKit无法映射

时间:2014-07-16 18:00:11

标签: ios objective-c restkit restkit-0.20

我似乎无法弄清楚如何映射以下JSON,我正在尝试从响应中映射hostedLargeUrl。我不知道该怎么办这个问题,如果我的信息不够详细,我想道歉。不太确定你需要什么类型的细节。 先感谢您。

images: [
{
imageUrlsBySize: {
90: "http://lh4.ggpht.com/ZXiwjS55Zk7oBu6GWaVr0HAqIPKumXwBfGtzsCWEFdrJSOXiCcC-I3TpUwrXBnP_DPNuBm-ib-4-3aXbs4mfXA=s90-c",
360: "http://lh4.ggpht.com/ZXiwjS55Zk7oBu6GWaVr0HAqIPKumXwBfGtzsCWEFdrJSOXiCcC-I3TpUwrXBnP_DPNuBm-ib-4-3aXbs4mfXA=s360-c"
},
hostedLargeUrl: "http://i.yummly.com/Pasta-with-garlicky-broccoli-rabe-305651-270310.l.jpg",
hostedSmallUrl: "http://i.yummly.com/Pasta-with-garlicky-broccoli-rabe-305651-270310.s.jpg"
}

这是我的代码:

+ (RKMapping *)recipeDetailMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RecipeDetails class]];

    [mapping addAttributeMappingsFromDictionary:@{

                                                  @"attribution.text" : @"attributionText",
                                                  @"images.hostedLargeUrl" : @"images"
                                                  }];



    [mapping addAttributeMappingsFromArray:@[@"ingredientLines",
                                             @"name",
                                             @"totalTime",

                                             ]];




    return mapping;

}

RecipeDetails

@property (nonatomic, copy) NSArray *ingredientLines;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *totalTime;
@property (nonatomic, copy) NSString *attributionText;
@property (nonatomic, copy) NSString *images;

最后一点代码

- (void)loadRecipeDetails
{
    NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

    RKMapping *mapping = [MappingProvder recipeDetailMapping];



    NSString *resourcePath = [NSString stringWithFormat:@"/v1/api/recipe/%@", self.recipeInfo.recipeId];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:resourcePath
                                                                                           keyPath:nil
                                                                                       statusCodes:statusCodeSet];










    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.yummly.com/v1/api/recipe/%@?_app_id=%@&_app_key=%@&requirePictures=true", self.recipeInfo.recipeId
                                       ,Yummly_APP_ID , Yummly_API_kEY ]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];



    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                        responseDescriptors:@[responseDescriptor]];

    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {




       self.recipeData = mappingResult.array;


        [self updateUI];


        [SVProgressHUD dismiss];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];





    } failure:^(RKObjectRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);
        NSLog(@"Response: %@", operation.HTTPRequestOperation.responseString);

        [SVProgressHUD showErrorWithStatus:@"Request Failed"];
    }];



    [operation start];





}

1 个答案:

答案 0 :(得分:0)

源数据在一个数组中,您需要以某种方式处理它。

选项1。 将属性更改为NSArray *images;

选项2。 添加自定义方法setImageArray:并实现它以提取第一个图像并存储它。然后更改映射以使用imageArray的目标键。

其他选项确实需要不同的对象图...