iphone - NSJSONSerialization Json 嵌套子

标签 iphone ios xcode json nsjsonserialization

我试图深入嵌套的 JSon 数组,我已经成功完成了上面的级别,但我无法弄清楚如何更深入。

我需要记录图像@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;
}

最佳答案

要遍历嵌套对象,您可以使用 valueForKeyPath 方法并使用点语法来深入层次结构。

像这样的东西会从你的 newsItem 字典中获取 url 值:

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

附言。如果您确实拥有以 @ 为前缀的属性,那么使用 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"];

关于iphone - NSJSONSerialization Json 嵌套子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15023221/

相关文章:

iphone - NSUndoManager 和 GLKit

iphone - 预先存在的核心数据数据

iphone - 无法将 NSString 放入 NSMutableArray 并将其存储在 NSUserDefaults 中

iphone - 在视网膜显示器上使用实际像素绘图

ios - 如果超过 30 分钟,如何将 NSString 时间转换为条件

c++ - 二进制表达式的无效操作数(常量点和常量点

ios - Xcode 在尝试将应用上传到 App Store 时产生 SSL 错误

iphone - 是否有 APN 的替代方案

iphone - 识别推送通知消息

ios - 如何反复检查互联网连接?