-4

i wan't to retrieve the image "url" from this Json : http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/search/person/name/kelly/fuzzy my Json have this format : results […images {small { url }}] can any one check with me this code please Any one to help me please

- (void)viewDidLoad {
[super viewDidLoad];


_tableData=[[NSMutableArray alloc]initWithCapacity:50] ;
_tab.delegate=self;
_tab.dataSource=self;
_tab.backgroundColor = [UIColor clearColor];


NSURL *url = [NSURL URLWithString:BaseURLStringg];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

  //  NSLog(@"JSON: %@", responseObject);
    NSDictionary *jsonDict = (NSDictionary *) responseObject;
    NSArray *products = [jsonDict objectForKey:@"results"];
    [products enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){

        NSString *name= [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"name"] ];

         //   poster= [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"url"] ];


        poster=[NSString stringWithFormat:@"%@ ", [[[obj objectForKey:@"images"]objectForKey:@"small"]objectForKey:@"url"]];

        NSLog(poster);

        NSDictionary *movieDictionary = @{@"name":name,
        @"url":poster};
        [self.tableData addObject:movieDictionary];
        [self.tab reloadData];



      }];        






    /*dispatch_sync(dispatch_get_main_queue(), ^{
     self.tableData=[[NSMutableArray alloc]initWithArray:responseObject];
     [self.tab reloadData]; });
     }*/



} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    NSLog(@"Error: %@", error);

   }];


   }





  -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return 1;
  }

  -(NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:(NSInteger)section{
  return [self.tableData count];
  }

  -(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  UITableViewCell* mycell=[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];

  // NSInteger nameindex=[_DB.arrColumnNames indexOfObject:@"name"];

//NSString* name=[[_myarray objectAtIndex:indexPath.row] objectAtIndex:nameindex];



 UILabel *name = [mycell.contentView viewWithTag:101];
 UIImageView *posterImage=[mycell.contentView viewWithTag:102];

/*    title.text= [_tableData objectAtIndex:indexPath.row];
 release_year.text= [_tableData objectAtIndex:indexPath.row];
 themoviedb.text= [_tableData objectAtIndex:indexPath.row];
 original_title.text= [_tableData objectAtIndex:indexPath.row];*/

NSDictionary *movieDictionary = self.tableData[indexPath.row];
name.text= movieDictionary[@"name"];




//mycell.textLabel.text=@"eererr";

NSString *finalURL = movieDictionary[@"url"];
finalURL = [finalURL stringByReplacingOccurrencesOfString:@"\\" withString:@""];
finalURL = [finalURL stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *cleanedUrl  = [finalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];





NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cleanedUrl]];


[posterImage setImage: [UIImage imageWithData:imageData]];





mycell.textLabel.textColor = [UIColor blackColor];



mycell.contentView.backgroundColor = [UIColor clearColor];
mycell.backgroundColor = [UIColor clearColor];
return mycell;
  }

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{


  }
  - (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
  }

  @end

this my Json file "results":[
{
"id":197753, "name":"Kelly Hu", "wikipedia_id":20646206, "freebase":"/m/03l7xc", "imdb":"nm0005026", "themoviedb":11024, "tvrage":497, "social":{
"twitter":{
"twitter_id":51649635, "link":"https://twitter.com/KellyHu" } }, "images":{
"small":{
"url":"http://static-api.guidebox.com/091414/thumbnails_people/29984-7234075409-1208944377-2556317393-small-135x200.jpg", "width":135, "height":200 },

  • 2
    You need to clean up your question. Remove all code that is not relevant to what you are asking. Format the code so it's more readable. Format the JSON. And finally make sure your question is clear and easy to understand. – drekka May 11 '16 at 01:41
  • @drekka my question is so clear and drag and drop my code or my json url in ur navigator u will understand what i mean – wassim wess May 11 '16 at 01:44
  • As the error message says - you are calling objectForKey on an NSArray and NSArray does not have a method called that. So you are extracting an array of arrays from your json but treating it as if it is an array of dictionaries. – Gruntcakes May 11 '16 at 02:54
  • @SausageMachine can you just correct it for me in the code cause i'm a beginner in iOS and i really need it – wassim wess May 11 '16 at 03:04
  • Its not possible without the exact json being known – Gruntcakes May 11 '16 at 03:08
  • 1
    @wassimwess Please see http://stackoverflow.com/help/mcve Your current question needs a lot of work. This is why your last few questions have done poorly. Most of the code you posted in this question has nothing to do with your issue as well as being poorly formatted. You haven't posted any details about the JSON you receive. You haven't pointed out which line of code causes the error. You haven't shown the relevant output of any of your `NSLog` statements. The goal is to make it easy for people to help you. – rmaddy May 11 '16 at 03:20
  • @SausageMachine This is the exact json http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/search/person/name/kelly/fuzzy – wassim wess May 11 '16 at 03:21
  • @rmaddy i posted the url of my json you can just open it and put it in a json to get a clear idea – wassim wess May 11 '16 at 03:23
  • Everything you need to know is [here](http://stackoverflow.com/a/27626036/581994). – Hot Licks May 11 '16 at 21:56
  • @HotLicks please check my code and tell me how to solve this line NSString *finalURL = movieDictionary[@"url"]; – wassim wess May 12 '16 at 00:33
  • Read the link. It's telling you that you cannot use "objectForKey" on an NSArray. – Hot Licks May 12 '16 at 00:36
  • (Your JSON is incomplete, by the way -- the `[]` and `{}` brackets don't balance.) – Hot Licks May 12 '16 at 00:39
  • And there is no element in `movieDictionary` called `@"url"`. – Hot Licks May 12 '16 at 00:41
  • @"http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/search/person/name/kelly/fuzzy" this is my complete son url can u tell me what to do i'm blockeed please help – wassim wess May 12 '16 at 00:50

1 Answers1

2
{
"id":753924,
"name":"Kelly Byrns",
"images":[]
}

The response json is incorrect. When has no image,it return an empty array,not a dictionary.

So you can either check obj[@"images"] type, or ask the server to return a dictionary all time.

NSDeveloper
  • 1,630
  • 15
  • 25