2013-02-22 76 views
1

我试图深入到一个嵌套的JSon数组,我已经成功地完成了上面的级别,但我不能解决如何获得更深入的。NSJSONSerialization Json嵌套Sub

我需要登录图片@url我附上了一个屏幕显示,以显示我需要它去的地方。

谢谢:)

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

遍历嵌套的对象,你可以使用valueForKeyPath方法和使用点语法钻到hieararchy。

像这样的东西会从你的字典newsItemurl值:

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

PS。如果您确实拥有以@为前缀的属性,那么您可能会因使用valueForKeyPath而陷入困境,因为@is a special token used as an operator。在这种情况下,你可以做这样的事情,而不是:

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 2013-02-22 11:44:17

+0

是的,只是意识到你有实际的'@'作为前缀。请参阅我的编辑答案 – Alladinian 2013-02-22 11:48:04

+0

@BrentFrench另请注意,在您的代码中,您将'tablethumb'视为字符串,而在您的json中将其视为字典... – Alladinian 2013-02-22 11:51:20

0

尝试访问值从字典这样,

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

这不会工作,因为那里已经有一个顶级的网址。不管怎么说,还是要谢谢你 – Brent 2013-02-22 11:59:19