0

我正在下载一个文件并尝试使用 JSON.parse,它应该返回{ dateTime: "2012-04-07T17:15:00.000-05:00", value: "1065.91" }我是否通过 JSON.parse 传递了正确的对象并正确地构造它?

代码:

var http = require("http");
var fs = require('fs');

var options = {
 host: 'waterdata.usgs.gov',
 port: 80,
 path: '/nwis/iv/?sites=02334400&period=P7D&format=json'
};

http.get(options, function(res) {
 var responseText = '';
 res.on('data', function(chunk) {
    responseText += chunk;
 });
 res.on('end', function() {
    fs.writeFile('response.txt', responseText.toString(), function(err) {
        if (err) throw err;
         console.log('It\'s saved!');
        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);
            }
        }
    });
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
4

1 回答 1

1

只需替换waterdata.usgs.govwaterservices.usgs.gov

于 2012-04-10T05:12:43.710 回答