Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple of observations:</p> <ol> <li><p>Your JSON is not well formed, missing a comma. It should be:</p> <pre><code>{ "noticias": [ { "titulo":"teste" }, { "titulo":"teste 2" } ] } </code></pre> <p>If you don't add that missing comma, you'll get an error from your parser. For example, <code>NSJSONSerialization</code> will report:</p> <blockquote> <p>Badly formed array around character 109</p> </blockquote></li> <li><p>To read the contents, you would load a <code>NSData</code> with the contents of the JSON and then parse it, for example:</p> <pre><code>NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url]; NSError *error = nil; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&amp;error]; if (error) NSLog(@"%@", error); else { NSArray *array = [dictionary objectForKey:@"noticias"]; NSDictionary *item = [array objectAtIndex:indexPath.row]; NSString *title = [item objectForKey:@"titulo"]; NSLog(@"%@", firstTitle); } </code></pre> <p>or, using the new, simpler syntax:</p> <pre><code>NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url]; NSError *error = nil; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&amp;error]; if (error) NSLog(@"%@", error); else { NSArray *array = dictionary[@"noticias"]; NSDictionary *item = array[indexPath.row]; NSString *title = item[@"titulo"]; NSLog(@"%@", title); } </code></pre></li> </ol> <hr> <p>First, define a property for your <code>noticias</code> in the <code>@interface</code> for your class:</p> <pre><code>@property (nonatomic, strong) NSArray *noticias; </code></pre> <p>Then read your <code>noticias</code>:</p> <pre><code>- (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:&amp;error]; if (error) { NSLog(@"%s: error=%@", __FUNCTION__, error); return; } self.noticias = [results objectForKey:@"noticias"]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }); } </code></pre> <p>Then</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; [self fetchNoticias]; } </code></pre> <p>and, finally, <code>cellForRowAtIndexPath</code>:</p> <pre><code>- (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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload