首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSJSONSerialization Json嵌套子

NSJSONSerialization Json嵌套子
EN

Stack Overflow用户
提问于 2013-02-22 19:32:37
回答 2查看 1.3K关注 0票数 1

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

我需要记录图像@url,我已经附加了一个屏幕显示,以显示我需要它去的地方。

谢谢:)

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-22 19:38:25

对于遍历嵌套对象,您可以使用valueForKeyPath方法并使用点语法向下钻取层次结构。

下面这样的代码将从您的newsItem字典中获取url值:

代码语言:javascript
复制
NSDictionary *newsItem = news[[indexPath row]];
NSString *thumbUrl = [newsItem valueForKeyPath:@"tablethumb.url"];

PS。如果你真的用@作为属性的前缀,那么从@ is a special token used as an operator开始使用valueForKeyPath可能会遇到麻烦。在这种情况下,您可以这样做:

代码语言:javascript
复制
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"];
票数 2
EN

Stack Overflow用户

发布于 2013-02-22 19:54:10

尝试像这样从字典中访问值,

代码语言:javascript
复制
NSLog(@"URL: %@",[newsItem objectForKey:@"@url"]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15023221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档