0

I've got a question regarding parsing a JSON response within iOS5.

Currently, I'm following this guide here to help me parse the JSON response returned from a third-party mapping service.

Everything works, except that the JSON response returned by the third-party server is somewhat different from the one shown in the guide itself.

In a nutshell, the overall structure of the entire JSON response looks something like this:

{  
    "directions": [....],  
    "messages": [....],  
    "routes":  
        {  
            "features": [  
                {  
                    "attributes": {....},  
                    "geometry":  
                        {  
                            "paths": [....]  
                        }  
                }  
            ]  
        }  
}  

This is the actual JSON query URL.

By using this line of code,

NSDictionary * jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

I am able to sucessfully get the jsonResponse dictionary to report that it has 3 key/value pairs, but my ultimate goal is to retrieve the array stored in 'routes.features.geometry.paths'.

This is my current code block that gets the final set of array values:

NSDictionary * jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray * jsonArray = [jsonResponse valueForKeyPath:@"routes.features.geometry.paths"];

jsonArray = [jsonArray objectAtIndex:0];
jsonArray = [jsonArray objectAtIndex:0];

I was wondering if anyone might have a better idea of how I should go about doing this in a more elegant fashion?

Thanks a lot in advance!

1 Answer 1

3

You can't just use it as JSON object because it will be working as JSON (Plain String) and you need to parse it so for your problem you can do like this to directly go to paths

NSArray *arr = [[[[jsonResponse objectForKey:@"routes"] objectForKey:@"features"] objectForKey:@"geometry"] objectForKey:@"paths"];

Now you can access your paths data from "arr" array

UPDATE:

NSArray *arr = [[[[[jsonResponse objectForKey:@"routes"] objectForKey:@"features"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"paths"];

as features element is an Array so traverse array first then goto its elements

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, I get an error when I do that: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xa0ce660'
[[[[[jsonResponse objectForKey:@"routes"] objectForKey:@"features"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"paths"]; this will run I guess as I saw the json again and your "features" node contain an Array which then traverse to Dictionary.
A quick note to check if json has Dictionary or Array, if you encounter this character "[" at any element than that element will traverse as Array and if you found "{" at any element than that element will traverse as Dictionary
A slight modification of your updated code works perfectly, thanks a lot! NSArray * jsonArray = [[[[[[jsonResponse objectForKey:@"routes"] objectForKey:@"features"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"paths"] objectAtIndex:0];

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.