2013-02-22 4 views
1

중첩 된 JSon 배열로 깊숙이 들어가려고 노력하고 있는데, 위의 레벨에서 성공적으로 수행했지만 더 깊이 이해할 수 없습니다.NSJSONSerialization Json Nested Sub

이미지를 기록해야합니다. @url 내가 필요한 곳을 보여주는 화면 쇼를 첨부했습니다.

감사합니다 :) 중첩 된 객체는 valueForKeyPath 방법을 사용하고 hieararchy를 드릴 다운 도트 구문을 사용할 수를 가로 지르는를 들어

json

- (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; 
} 

답변

2

. 이 같은

무언가가 당신의 newsItem 사전에서 url 값을 가져 오는 것입니다 :

NSDictionary *newsItem = news[[indexPath row]]; 
NSString *thumbUrl = [newsItem valueForKeyPath:@"tablethumb.url"]; 

PS합니다. 속성이 실제로 @ 인 경우 @is a special token used as an operator부터 valueForKeyPath을 사용하면 문제가 발생할 수 있습니다. 이 경우 대신이 같은 뭔가를 할 수 :

NSDictionary *newsItem = news[[indexPath row]]; 
id tablethumb = [newsItem objectForKey:@"tablethumb"]; 
NSString *thumbUrl = @""; 
// Check if not null and access the @url 
if (tablethumb != [NSNull null]) 
    thumbUrl = tablethumb[@"@url"]; 
+0

감사를 사용해보십시오. image = (null), 그런 다음 @ "tablethumb. @ url"을 시도하고 NSUnkownKeyException의 마지막 로그에서 오류가 발생했습니다. – Brent

+0

예, 접두사로 실제'@ '가 있음을 알았습니다. 내 편집 된 답변을 참조하십시오 – Alladinian

+0

@BrentFrench 또한 귀하의 코드에서 귀하의 json에서 자사의 사전 동안 문자열로'tablethumb'를 치료하고있어 ... – Alladinian

0

는이 같은 사전에서 액세스 값으로 나는 위의 코드를 시도하고 내 콘솔 말했다 빠른 응답

NSLog(@"URL: %@",[newsItem objectForKey:@"@url"]); 
+0

이것은 이미 최상위 수준의 URL로 작동합니다. 어쨌든 고마워. – Brent

관련 문제