我正在尝试从天气应用返回的JSON访问JavaScript中的嵌套数组。但是,如果控制台未返回cannot return property of 'x' of undefined
,我似乎无法访问任何数据。我可以肯定地说问题出在我与result
的互动方式上,但是我不确定。有谁知道我如何从嵌套数组中正确访问数据?
这是我目前无法使用的代码:
const weather = require('weather-js');
var val;
var temp;
var final;
weather.find({search: 'Oceanside, CA', degreeType: 'F'}, function(err, result){
if (err) console.log(err);
obj = JSON.stringify(result, null, 2);
temp = result[2].current.temperature;
console.log(temp);
final = result[1].location.name;
console.log(final)
});
这是我要与之交互的JSON:
[
{
"location": {
"name": "Oceanside, CA",
"lat": "33.197",
"long": "-117.381",
"timezone": "-8",
"alert": "",
"degreetype": "F",
"imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
},
"current": {
"temperature": "55",
"skycode": "31",
"skytext": "Mostly Clear",
"date": "2018-12-18",
"observationtime": "22:15:00",
"observationpoint": "Oceanside, CA",
"feelslike": "55",
"humidity": "90",
"winddisplay": "3 mph Southwest",
"day": "Tuesday",
"shortday": "Tue",
"windspeed": "3 mph",
"imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif"
},
"forecast": [
{
"low": "46",
"high": "64",
"skycodeday": "29",
"skytextday": "Partly Cloudy",
"date": "2018-12-17",
"day": "Monday",
"shortday": "Mon",
"precip": ""
},
{
"low": "45",
"high": "65",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-18",
"day": "Tuesday",
"shortday": "Tue",
"precip": "0"
},
{
"low": "44",
"high": "67",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-19",
"day": "Wednesday",
"shortday": "Wed",
"precip": "0"
},
{
"low": "47",
"high": "69",
"skycodeday": "30",
"skytextday": "Partly Sunny",
"date": "2018-12-20",
"day": "Thursday",
"shortday": "Thu",
"precip": "0"
},
{
"low": "47",
"high": "65",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-21",
"day": "Friday",
"shortday": "Fri",
"precip": "0"
}
]
}
]
这个问题不是访问/进程(嵌套)对象,数组或JSON的重复,因为据我所见,该页面上的答案处理的是多个元素的数组,而这个问题没有。
答案 0 :(得分:0)
//try this its working
var j = [{
"location": {
"name": "Oceanside, CA",
"lat": "33.197",
"long": "-117.381",
"timezone": "-8",
"alert": "",
"degreetype": "F",
"imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
},
"current": {
"temperature": "55",
"skycode": "31",
"skytext": "Mostly Clear",
"date": "2018-12-18",
"observationtime": "22:15:00",
"observationpoint": "Oceanside, CA",
"feelslike": "55",
"humidity": "90",
"winddisplay": "3 mph Southwest",
"day": "Tuesday",
"shortday": "Tue",
"windspeed": "3 mph",
"imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif"
},
"forecast": [{
"low": "46",
"high": "64",
"skycodeday": "29",
"skytextday": "Partly Cloudy",
"date": "2018-12-17",
"day": "Monday",
"shortday": "Mon",
"precip": ""
},
{
"low": "45",
"high": "65",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-18",
"day": "Tuesday",
"shortday": "Tue",
"precip": "0"
},
{
"low": "44",
"high": "67",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-19",
"day": "Wednesday",
"shortday": "Wed",
"precip": "0"
},
{
"low": "47",
"high": "69",
"skycodeday": "30",
"skytextday": "Partly Sunny",
"date": "2018-12-20",
"day": "Thursday",
"shortday": "Thu",
"precip": "0"
},
{
"low": "47",
"high": "65",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2018-12-21",
"day": "Friday",
"shortday": "Fri",
"precip": "0"
}
]
}];
console.log(j[0].current.temperature);
console.log(j[0].location.name);
答案 1 :(得分:0)
这很简单。
假设JavaScript中的数组定义为var myArray=[]
您会注意到您的JSON具有一个外部[],在其中您具有1个外部{},这意味着它在数组中的索引为0处具有1个元素。
因此,不要做result[2].current.temperature;
,而要做result[0].current.temperature
;
使用result[2]
会给您未定义的内容,因为它不存在!