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);
}
id resultis probably an array or dictionary of arrays or dictionaries. We need to figure that out so we can go after the data.