1

我是 xcode 的新手,我需要帮助。

我有这个json:

{
    "noticias": [
        {
             "titulo":"teste"
        }
        {
             "titulo":"teste 2"
        }
    ]
}

我是怎么得到结果的?我试过了

NSDictionary *not = [noticias objectAtIndex:indexPath.row];

但我得到了一个错误,因为我有关键的“noticias”


更新:这是尝试的代码,基于迄今为止收到的答案。我有这个:

- (void)fetchNoticias
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"http://www.site.com/json"]];

        NSError* error;

        noticias = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

然后

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fetchNoticias];
}

和我有问题的决赛

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NoticiasCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSArray *noticia = [noticias objectForKey:@"noticias"];
    NSDictionary *not = [noticias objectAtIndex:indexPath.row];
    NSString *firstTitle = [not objectForKey:@"titulo"];

    cell.textLabel.text = firstTitle;
    //cell.detailTextLabel.text = subtitulo;

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}

在线问题:

NSArray *noticia = [noticias objectForKey:@"noticias"];
4

3 回答 3

2

几点观察:

  1. 您的 JSON 格式不正确,缺少逗号。它应该是:

    {
        "noticias": [
                     {
                     "titulo":"teste"
                     },
                     {
                     "titulo":"teste 2"
                     }
                     ]
    }
    

    如果你不添加缺少的逗号,你会从你的解析器中得到一个错误。例如,NSJSONSerialization将报告:

    字符 109 周围的数组格式错误

  2. 要读取内容,您将NSData使用 JSON 的内容加载 a,然后对其进行解析,例如:

    NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    if (error)
        NSLog(@"%@", error);
    else
    {
        NSArray *array = [dictionary objectForKey:@"noticias"];
        NSDictionary *item = [array objectAtIndex:indexPath.row];
        NSString *title = [item objectForKey:@"titulo"];
    
        NSLog(@"%@", firstTitle);
    }
    

    或者,使用新的、更简单的语法:

    NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    if (error)
        NSLog(@"%@", error);
    else
    {
        NSArray *array = dictionary[@"noticias"];
        NSDictionary *item = array[indexPath.row];
        NSString *title = item[@"titulo"];
    
        NSLog(@"%@", title);
    }
    

首先,为您的类定义一个noticias属性@interface

@property (nonatomic, strong) NSArray *noticias;

然后阅读您的noticias

- (void)fetchNoticias
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"http://www.site.com/json"]];

        NSError* error = nil;

        NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data
                                                                options:kNilOptions
                                                                  error:&error];

        if (error) 
        {
            NSLog(@"%s: error=%@", __FUNCTION__, error);
            return;
        }

        self.noticias = [results objectForKey:@"noticias"];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

然后

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fetchNoticias];
}

最后,cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NoticiasCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *noticia = [self.noticias objectAtIndex:indexPath.row];
    NSString *firstTitle = [noticia objectForKey:@"titulo"];

    cell.textLabel.text = firstTitle;
    //cell.detailTextLabel.text = subtitulo;

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}
于 2013-05-30T02:40:30.017 回答
1

尝试这个

      NSArray *array = [dictionary objectForKey:@"noticias"];
      for(int i=0;i<[array count];i++)
      {
       NSDictionary *item = [array objectAtIndex:i];
      NSString *title = [item objectForKey:@"titulo"];
       NSLog(@"%@",title);
      }

希望它对你有帮助。

于 2013-05-30T07:54:00.523 回答
1

假设您的 jsonData 是“响应”

NSError* error;
NSDictionary* ResponseDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSArray* noticias = [ResponseDic objectForKey:@"noticias"];
NSDictionary* not = [noticias objectAtIndex:indexPath.row];

希望这对你有帮助。

于 2013-05-30T02:41:13.790 回答