我正在尝试使用正在尝试的Node.js应用程序从AccuWeather API中获取数据。我使用JSON.parse()
方法解析了响应。但是,当我尝试访问某些嵌套对象时,它是一个undefined
值。我对这一切还很陌生,不知道发生了什么或如何处理。
我从服务器获得的响应如下
{"LocalObservationDateTime":"2019-03-27T12:30:00+05:30","EpochTime":1553670000,"WeatherText":"Partly sunny","WeatherIcon":3,"HasPrecipitation":false,"PrecipitationType":null,"IsDayTime":true,"Temperature":{"Metric":{"Value":33.4,"Unit":"C","UnitType":17},"Imperial":{"Value":92.0,"Unit":"F","UnitType":18}},"RealFeelTemperature":{"Metric":{"Value":40.1,"Unit":"C","UnitType":17},"Imperial":{"Value":104.0,"Unit":"F","UnitType":18}},"RealFeelTemperatureShade":{"Metric":{"Value":37.4,"Unit":"C","UnitType":17},"Imperial":{"Value":99.0,"Unit":"F","UnitType":18}},"RelativeHumidity":61,"DewPoint":{"Metric":{"Value":25.1,"Unit":"C","UnitType":17},"Imperial":{"Value":77.0,"Unit":"F","UnitType":18}},"Wind":{"Direction":{"Degrees":315,"Localized":"NW","English":"NW"},"Speed":{"Metric":{"Value":16.7,"Unit":"km/h","UnitType":7},"Imperial":{"Value":10.4,"Unit":"mi/h","UnitType":9}}},"WindGust":{"Speed":{"Metric":{"Value":12.4,"Unit":"km/h","UnitType":7},"Imperial":{"Value":7.7,"Unit":"mi/h","UnitType":9}}},"UVIndex":11,"UVIndexText":"Extreme","Visibility":{"Metric":{"Value":16.1,"Unit":"km","UnitType":6},"Imperial":{"Value":10.0,"Unit":"mi","UnitType":2}},"ObstructionsToVisibility":"","CloudCover":50,"Ceiling":{"Metric":{"Value":9144.0,"Unit":"m","UnitType":5},"Imperial":{"Value":30000.0,"Unit":"ft","UnitType":0}},"Pressure":{"Metric":{"Value":1012.0,"Unit":"mb","UnitType":14},"Imperial":{"Value":29.88,"Unit":"inHg","UnitType":12}},"PressureTendency":{"LocalizedText":"Falling","Code":"F"},"Past24HourTemperatureDeparture":{"Metric":{"Value":-0.2,"Unit":"C","UnitType":17},"Imperial":{"Value":0.0,"Unit":"F","UnitType":18}},"ApparentTemperature":{"Metric":{"Value":40.6,"Unit":"C","UnitType":17},"Imperial":{"Value":105.0,"Unit":"F","UnitType":18}},"WindChillTemperature":{"Metric":{"Value":33.3,"Unit":"C","UnitType":17},"Imperial":{"Value":92.0,"Unit":"F","UnitType":18}},"WetBulbTemperature":{"Metric":{"Value":27.3,"Unit":"C","UnitType":17},"Imperial":{"Value":81.0,"Unit":"F","UnitType":18}},"Precip1hr":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"PrecipitationSummary":{"Precipitation":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"PastHour":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past3Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past6Hours":{"Metric":{"Value":0.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.0,"Unit":"in","UnitType":1}},"Past9Hours":{"Metric":{"Value":1.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.04,"Unit":"in","UnitType":1}},"Past12Hours":{"Metric":{"Value":2.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.08,"Unit":"in","UnitType":1}},"Past18Hours":{"Metric":{"Value":3.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.12,"Unit":"in","UnitType":1}},"Past24Hours":{"Metric":{"Value":3.0,"Unit":"mm","UnitType":3},"Imperial":{"Value":0.12,"Unit":"in","UnitType":1}}},"TemperatureSummary":{"Past6HourRange":{"Minimum":{"Metric":{"Value":29.0,"Unit":"C","UnitType":17},"Imperial":{"Value":84.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":33.4,"Unit":"C","UnitType":17},"Imperial":{"Value":92.0,"Unit":"F","UnitType":18}}},"Past12HourRange":{"Minimum":{"Metric":{"Value":26.4,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":33.4,"Unit":"C","UnitType":17},"Imperial":{"Value":92.0,"Unit":"F","UnitType":18}}},"Past24HourRange":{"Minimum":{"Metric":{"Value":26.4,"Unit":"C","UnitType":17},"Imperial":{"Value":80.0,"Unit":"F","UnitType":18}},"Maximum":{"Metric":{"Value":34.9,"Unit":"C","UnitType":17},"Imperial":{"Value":95.0,"Unit":"F","UnitType":18}}}},"MobileLink":"http://m.accuweather.com/en/in/cheranallur/2875231/current-weather/2875231?lang=en-us","Link":"http://www.accuweather.com/en/in/cheranallur/2875231/current-weather/2875231?lang=en-us"}
我正在使用的代码如下
request('api_url', function(error, response, body) {
var jsonObj = JSON.parse(body);
console.log(jsonObj.EpochTime); // This works fine.
console.log(jsonObj.WeatherText); // Also this.
console.log(jsonObj.Speed.Metric.Value); // This is the code that is crashing.
}
我收到的错误消息如下
/Users/abbinvarghese/Desktop/weather-history/app.js:77
windSpeed: jsonObj.Speed.Metric.Value, // in km/h
^
TypeError: Cannot read property 'Metric' of undefined
at Object.<anonymous> (/Users/abbinvarghese/Desktop/weather-history/app.js:77:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
答案 0 :(得分:3)
您错过了function [y] = brent(f, x0, x1, max_iter, tolerance)
fx0 = f(x0)
fx1 = f(x1)
......
end
Wind
答案 1 :(得分:0)
我认为应该是jsonObj.Wind.Speed.Metric.Value
"Wind" : {
"Direction" : {
"Degrees" : 315,
"Localized" : "NW",
"English" : "NW"
},
"Speed" : {
"Metric" : {
"Value" : 16.7,
"Unit" : "km/h",
"UnitType" : 7
},
"Imperial" : {
"Value" : 10.4,
"Unit" : "mi/h",
"UnitType" : 9
}
}
},
答案 2 :(得分:0)
Speed是Json对象中Wind的属性,您可以通过打印Javascript对象的属性来轻松调试它们。参见下面的代码
var jsonObj = JSON.parse(body);
console.log(jsonObj.EpochTime); // This works fine.
console.log(jsonObj.WeatherText); // Also this.
// console.log(jsonObj.Speed.Metric.Value); // This is the code that is crashing.
console.log(jsonObj.Wind.Speed.Metric.Value); // Speed is a property of Wind
console.log("Json Object Properties");
var output = '';
for (var property in jsonObj) {
output += property + ': ' + jsonObj[property] + '; ';
}
console.log(output);