我已下载我的 JSON 数据,但无法访问特定对象。从我的 JSON 数据中,我试图value
从variableName = "Elevation of reservoir water surface above datum, ft";
这是我的代码:
- (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);
}
}
编辑:打印输出的数据太多,但这是我在 JS 中访问对象的方式。我似乎无法编写在 Obj-C 中执行相同操作的 for 语句:
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);
}