OpenWeatherMap API 仅预报几天

OpenWeatherMap API forecast for only days

问题是当我获取 URL 以获取预测的 JSON 文件时,它为我提供了一个 40 对象数组(每 3 小时预测一次,持续 5 天) 现在我只想显示每天的天气预报,而不是每 3 小时的天气预报。

如何使用 JavaScript 完成?

数组:

{…}
​
city: Object { id: 2643743, name: "London", country: "GB", … }
​
cnt: 40
​
cod: "200"
​
list: (40) […]
​​
0: Object { dt: 1580148000, main: {…}, dt_txt: "2020-01-27 18:00:00", … }
​​
1: Object { dt: 1580158800, main: {…}, dt_txt: "2020-01-27 21:00:00", … }
​​
2: Object { dt: 1580169600, main: {…}, dt_txt: "2020-01-28 00:00:00", … }
​​
3: Object { dt: 1580180400, main: {…}, dt_txt: "2020-01-28 03:00:00", … }
​​
4: Object { dt: 1580191200, main: {…}, dt_txt: "2020-01-28 06:00:00", … }
​​
5: Object { dt: 1580202000, main: {…}, dt_txt: "2020-01-28 09:00:00", … }
​​
6: Object { dt: 1580212800, main: {…}, dt_txt: "2020-01-28 12:00:00", … }
​​
7: Object { dt: 1580223600, main: {…}, dt_txt: "2020-01-28 15:00:00", … }
​​
8: Object { dt: 1580234400, main: {…}, dt_txt: "2020-01-28 18:00:00", … }
​​
9: Object { dt: 1580245200, main: {…}, dt_txt: "2020-01-28 21:00:00", … }
​​
10: Object { dt: 1580256000, main: {…}, dt_txt: "2020-01-29 00:00:00", … }
​​
11: Object { dt: 1580266800, main: {…}, dt_txt: "2020-01-29 03:00:00", … }
​​
12: Object { dt: 1580277600, main: {…}, dt_txt: "2020-01-29 06:00:00", … }
​​
13: Object { dt: 1580288400, main: {…}, dt_txt: "2020-01-29 09:00:00", … }
​​
14: Object { dt: 1580299200, main: {…}, dt_txt: "2020-01-29 12:00:00", … }
​​
15: Object { dt: 1580310000, main: {…}, dt_txt: "2020-01-29 15:00:00", … }
​​
16: Object { dt: 1580320800, main: {…}, dt_txt: "2020-01-29 18:00:00", … }
​​
17: Object { dt: 1580331600, main: {…}, dt_txt: "2020-01-29 21:00:00", … }
​​
18: Object { dt: 1580342400, main: {…}, dt_txt: "2020-01-30 00:00:00", … }
​​
19: Object { dt: 1580353200, main: {…}, dt_txt: "2020-01-30 03:00:00", … }
​​
20: Object { dt: 1580364000, main: {…}, dt_txt: "2020-01-30 06:00:00", … }
​​
21: Object { dt: 1580374800, main: {…}, dt_txt: "2020-01-30 09:00:00", … }
​​
22: Object { dt: 1580385600, main: {…}, dt_txt: "2020-01-30 12:00:00", … }
​​
23: Object { dt: 1580396400, main: {…}, dt_txt: "2020-01-30 15:00:00", … }
​​
24: Object { dt: 1580407200, main: {…}, dt_txt: "2020-01-30 18:00:00", … }
​​
25: Object { dt: 1580418000, main: {…}, dt_txt: "2020-01-30 21:00:00", … }
​​
26: Object { dt: 1580428800, main: {…}, dt_txt: "2020-01-31 00:00:00", … }
​​
27: Object { dt: 1580439600, main: {…}, dt_txt: "2020-01-31 03:00:00", … }
​​
28: Object { dt: 1580450400, main: {…}, dt_txt: "2020-01-31 06:00:00", … }
​​
29: Object { dt: 1580461200, main: {…}, dt_txt: "2020-01-31 09:00:00", … }
​​
30: Object { dt: 1580472000, main: {…}, dt_txt: "2020-01-31 12:00:00", … }
​​
31: Object { dt: 1580482800, main: {…}, dt_txt: "2020-01-31 15:00:00", … }
​​
32: Object { dt: 1580493600, main: {…}, dt_txt: "2020-01-31 18:00:00", … }
​​
33: Object { dt: 1580504400, main: {…}, dt_txt: "2020-01-31 21:00:00", … }
​​
34: Object { dt: 1580515200, main: {…}, dt_txt: "2020-02-01 00:00:00", … }
​​
35: Object { dt: 1580526000, main: {…}, dt_txt: "2020-02-01 03:00:00", … }
​​
36: Object { dt: 1580536800, main: {…}, dt_txt: "2020-02-01 06:00:00", … }
​​
37: Object { dt: 1580547600, main: {…}, dt_txt: "2020-02-01 09:00:00", … }
​​
38: Object { dt: 1580558400, main: {…}, dt_txt: "2020-02-01 12:00:00", … }
​​
39: Object { dt: 1580569200, main: {…}, dt_txt: "2020-02-01 15:00:00", … }
​​
length: 40
​​
<prototype>: Array []
​
message: 0
​
<prototype>: Object { … }
weather.js:78:11

而且我需要它与用户获取数据的日期无关。

我正在使用免费版的 openweathermap。

那么您使用的 api 类型不正确。试试这个

api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=xml&units=metric&cnt=7

link here

for (var i = 0; i < weatherForecast.list.length; i+=8) {
  console.log(weatherForecast.list[i].dt_txt);
}

这是每天都给的

因为有40个对象的数组,5天,5*8=40

5=天