我不想从这个Json中检索图像“url”: http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/search/person/name/kelly/fuzzy 我的Json有这种格式: 结果[... images {small {url}}] 任何人都可以和我一起检查这个代码 请帮助我的任何人
- (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
这是我的Json文件
“结果”:答案 0 :(得分:1)
{
"id":753924,
"name":"Kelly Byrns",
"images":[]
}
响应json不正确。当没有图像时,它返回一个空数组,不是字典。
因此,您可以检查obj[@"images"]
类型,或要求服务器始终返回字典。