Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Parsing JSON data - a dictionary with only 1 key?

I am downloading JSON data for display in a UILabel. It appears that the JSON data I am downloading via an API is a dictionary of one key, where the "value" of the key contains a sub-dictionary.

For starters, I am attempting to display only one value of the JSON data.

Here is the raw JSON output (pretty simple):

{"weatherObservation":{"clouds":"n/a","weatherCondition":"n/a","observation":"KRNM 031753Z VRB04KT 10SM CLR 13/04 A3012 RMK AO2 SLP197 T01330044 10139 21006 50001","ICAO":"KRNM","seaLevelPressure":1019.7,"elevation":427,"countryCode":"US","lng":-116.91666666666667,"temperature":"13.3","dewPoint":"4.4","windSpeed":"04","humidity":54,"stationName":"Ramona, Ramona Airport","datetime":"2014-04-03 17:53:00","lat":33.03333333333333}}

And the formatted JSON data (more readable):

{"weatherObservation":
{
  "ICAO": "KRNM",
  "clouds": "n/a",
  "countryCode": "US",
  "datetime": "2014-04-03 17:53:00",
  "dewPoint": "4.4",
  "elevation": 427,
  "humidity": 54,
  "lat": 33.03333333333333,
  "lng": -116.91666666666667,
  "observation": "KRNM 031753Z VRB04KT 10SM CLR 13/04     A3012 RMK AO2 SLP197 T01330044 10139 21006 50001",
  "seaLevelPressure": 1019.7,
  "stationName": "Ramona, Ramona Airport",
  "temperature": "13.3",
  "weatherCondition": "n/a",
  "windSpeed": "04"
}}

For starters I just want to display the valueForKey @"observation"

But it seems like this data is some kind of "double dictionary" how do I drill down into the "sub dictionary"?

I have no trouble handling the JSON download, I accomplish it in the viewDIdAppear method:

NSURL *weatherURL = [NSURL URLWithString:@"http://api.geonames.org/findNearByWeatherJSON?lat=33&lng=-116.87&username=InsertAPIKeyHere"];
    NSData *weatherData = [NSData dataWithContentsOfURL:weatherURL];
    NSError *error = nil;

    NSDictionary *weatherObject = [NSJSONSerialization JSONObjectWithData:weatherData options:0 error:&error];
    NSLog(@"Parsed data: %@", weatherObject);

    self.weatherLabel.text =[weatherObject objectForKey:@"observation"];

This code returns nothing - I'm not actually accessing the objectForKey "observation" - it is one level down. Any ideas? I think I'm missing a simple solution here.

2 Answers

When you want to view a nested dictionary you should be able to link indices since an associative array is still an array. You can do something like this:

putReturnedObjectHere['weatherObservation']['observation'];

Hopefully this helps a bit.

Success!

I nested the calls as such:

//download and create the NSDictionary

 NSURL *weatherURL = [NSURL URLWithString:@"http://api.geonames.org/findNearByWeatherJSON?lat=33&lng=-116.87&username=InsertAPIKeyHere"];
    NSData *weatherData = [NSData dataWithContentsOfURL:weatherURL];
    NSError *error = nil;
    NSDictionary *weatherObject = [NSJSONSerialization JSONObjectWithData:weatherData options:0 error:&error];

//access the nested NSDictionary with nested calls    
    NSString *metar = [[weatherObject valueForKey:@"weatherObservation"] valueForKey:@"observation"];

//send the NSString to a UITextLabel outlet:   
    self.weatherLabel.text = metar;

Your comment certainly helped. Thanks.

Glad you got it working!