0

I've downloaded my JSON Data, but I'm having trouble accessing a specific object. From my JSON data, I'm trying to pull the most recent value from variableName = "Elevation of reservoir water surface above datum, ft";

Here is my code:

- (void)viewWillAppear:(BOOL)animated {
[super viewDidAppear:animated];

NSURL *url = [NSURL URLWithString:@"http://waterservices.usgs.gov/nwis/iv/?sites=02334400&period=P7D&format=json"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:url];
if (jsonData != nil) {
    NSError *error = nil;

    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error: &error];

    if (error == nil)
        NSLog(@"%@", result);
}
}

Edited: It's too much data to print the output, but here is how I access the object in JS. I can't seem to write a working for statement that will do the same in Obj-C:

 var d = JSON.parse(responseText);
    for (var i = 0; i < d.value.timeSeries.length; i++) {
        if (d.value.timeSeries[i].variable.variableName == 'Elevation of reservoir      water surface above datum, ft') {
            var result = d.value.timeSeries[i].values[0].value[d.value.timeSeries[i].values[0].value.length - 1];
            console.log(result);
        }
3
  • NSLog your result to the console and post that. Your id result is probably an array or dictionary of arrays or dictionaries. We need to figure that out so we can go after the data. Commented Apr 11, 2012 at 22:34
  • JSON maps directly to a layered structure of NSArrays and NSDictionarys. You need to figure out what the actual structure is (which depends on the JSON data) and then just peel the layers of the onion. Commented Apr 11, 2012 at 22:47
  • The output is over 100000 characters. I'm having trouble duplicating this JS FOR statement in Obj-C. Commented Apr 11, 2012 at 23:00

2 Answers 2

2

This is pretty ugly but it should give you something to start with:

NSArray *timeSeries = [JSON valueForKeyPath:@"value.timeSeries"];

[timeSeries enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSString *variableName = [obj valueForKeyPath:@"variable.variableName"];

    if ([variableName isEqualToString:@"Elevation of reservoir water surface above datum, ft"]) {

        NSArray      *values = [obj valueForKey:@"values"];
        NSDictionary *value  = [values objectAtIndex:0];

        values = [value objectForKey:@"value"];
        value  = [values lastObject];

        NSLog(@"%@", [value objectForKey:@"value"]);
    }
}];

Note

There is no validation/range checking of any kind I'll leave that as an exercise for you to do

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

Comments

0

One thing that you may consider is using a tool to generate model classes for you. That way you can use dot accessors to make your life a little bit easier. In the Mac App Store JSON Accelerator or Objectify are pretty good options. You then pipe the NSDictionary into those model classes and it's pretty easy.

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.