0

I have an array (called results):

@interface FourViewController : UIViewController
{    
    NSArray *results;
    NSMutableData *data;
}

NSURL *url = [NSURL URLWithString:@"http://website/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
}

When I run this for loop it returns all keys in the array correctly with each JSON string in:

int i;

for (i = 0; i < [results count]; i++) {
     NSLog(@"Result: %i = %@", i, results[i]);
}

However inside the for loop I need something like this but I cannot find documentation to do it:

int i;

for (i = 0; i < [results count]; i++) {
    location.latitude = results[i][lat];
    location.longitude = results[i][long];
    myAnn.coordinate = location;
    myAnn.title = results[i][title];
    myAnn.subtitle = results[i][strap];
    [locations addObject:myAnn];
}

lat, long, title and strap are all JSON keys / id's but i cannot access them; is there a way for me to access each key within each iteration inside the array?

3
  • What is results? Is it an instance of NSArray or NSDictionary? Commented Mar 17, 2013 at 18:45
  • NSArray I think (in the header file, which is what you are talking about I think? I'm still getting my head around obj-c) Commented Mar 17, 2013 at 18:48
  • Even if you declare results as an NSArray, you might not be getting that at runtime from JSONObjectWithData (welcome to Objective C) depending on how the JSON data is structured. You will want to validate that. Commented Mar 17, 2013 at 18:51

2 Answers 2

2
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:lat] floatValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:long] floatValue];

Hope this helps...

myAnn.title = (double)[[[results objectAtIndex:i] objectForKey:title] floatValue];
myAnn.subtitle = (double)[[[results objectAtIndex:i] objectForKey:strap] floatValue];
Sign up to request clarification or add additional context in comments.

10 Comments

Hi there! I will give this a whirl, looks promising :) just out of interest, objectForKey: is that just a JSON instruction or does it apply to normal arrays too?
Ahh okay thanks! Should the objectForKey key be in quotes as I am getting an error, should it be objectForKey: @"title"; ? thanks!
Thanks for the help so far! I am getting this error, imagine it is a datatype issue? i.imgur.com/50bICTO.png
do a casting-> like this: (double)[[results objectAtIndex:i] objectForKey:lat];
[[[results objectAtIndex:i] objectForKey:lat] doubleValue];
|
1

I think you're almost there with your answer already, as you mention the word "keys".

Chances are high (and I need to verify this by dropping your code into a test project) that each of the objects in the array are "NSDictionary" objects, from which you can get values for the "lat", "long", "title" keys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.