중첩 된 JSon 배열로 깊숙이 들어가려고 노력하고 있는데, 위의 레벨에서 성공적으로 수행했지만 더 깊이 이해할 수 없습니다.NSJSONSerialization Json Nested Sub
이미지를 기록해야합니다. @url 내가 필요한 곳을 보여주는 화면 쇼를 첨부했습니다.
감사합니다 :) 중첩 된 객체는 valueForKeyPath
방법을 사용하고 hieararchy를 드릴 다운 도트 구문을 사용할 수를 가로 지르는를 들어
- (void)viewDidLoad
{
[super viewDidLoad];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://api.storageroomapp.com/accounts/511a4f810f66026b640007b8/collections/511a51580f66023bff000ce9/entries.json?auth_token=Zty6nKsFyqpy7Yp5DP1L&preview_api=1"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *) connection{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSDictionary *arrayDictionary = dictionary[@"array"];
news = arrayDictionary[@"resources"];
[tableView reloadData];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The data could not be downloaded - please make sure you're connected to either 3G or Wi-FI" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [news count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *newsItem = news[[indexPath row]];
NSString *title = newsItem[@"title"];
NSString *date = newsItem[@"date"];
NSString *thumb = newsItem[@"tablethumb"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
[[cell textLabel] setText:title];
[[cell detailTextLabel] setText:date];
if((NSNull *)thumb == [NSNull null]){
NSLog(@"no image");
} else{
NSLog(@ "image = %@", thumb);
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
감사를 사용해보십시오. image = (null), 그런 다음 @ "tablethumb. @ url"을 시도하고 NSUnkownKeyException의 마지막 로그에서 오류가 발생했습니다. – Brent
예, 접두사로 실제'@ '가 있음을 알았습니다. 내 편집 된 답변을 참조하십시오 – Alladinian
@BrentFrench 또한 귀하의 코드에서 귀하의 json에서 자사의 사전 동안 문자열로'tablethumb'를 치료하고있어 ... – Alladinian