2014-07-16 252 views
0

我似乎無法弄清楚如何映射下面的JSON,我試圖從響應映射hostedLargeUrl。我不知道該怎麼做這個問題,並想道歉,如果我的信息不夠詳細。不太確定你需要什麼類型的細節。 預先感謝您。RestKit無法映射

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





} 

回答

0

源數據是在數組中最後一位,你需要處理以某種方式。

選項1. 更改屬性NSArray *images;

選項2 添加自定義方法setImageArray:並執行它,以提取所述第一圖像和存儲它。然後將映射更改爲使用imageArray的目標密鑰。

其他選項確實需要不同的對象圖...

+0

謝謝你的工作就像一個魅力。 – TylerMiller47