Search code examples
iosnsjsonserialization

Iterating a NSArray populated from JSON


I am initialising the following NSArray using JSON;

categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

I can retrieve the number of objects in the array logging it:

NSLog(@"NUMERO DE EMPRESAS = %lu", (unsigned long)[categorias count]);

The expected number of objects is 24, the log is correct.

NUMERO DE EMPRESAS = 24

Now, what I need is to iterate the 24 objects and show the value for the key 'latitud'

for (int i=0; i<[categorias count]; i++) {
    NSString *lalitudText = x; 
    NSLog (@"EL VALOR ES %@", latitudText);  
}

What should I put instead of x, to obtain the value?


Solution

  • Using the old syntax you can put this:

    NSString *lalitudText = [[categorias objectAtIndex:i] objectForKey:@"latitude"]; 
    

    The new syntax lets you do this:

    NSString *lalitudText = categorias[i][@"latitude"];