使用OpenWeather预报API获取5天逐日天气返回异常如何处理
核心问题根源
当前调用的https://api.openweathermap.org/data/2.5/forecast接口本身为3小时粒度的逐时段预报接口:
- 接口默认返回未来5天内每间隔3小时的天气数据,总计40条记录
- 参数
cnt:5仅控制返回从头开始的5条3小时时段数据,因此拿到的5条结果全是同一天不同时间点的预报,无法凑齐未来5天的逐日数据。
可落地调整方案
方案1:切换至官方逐日预报接口(推荐)
直接更换接口为官方提供的逐日预报端点,接口内部已经完成同日多时段数据的聚合,可直接拿到对应日期的最高温、最低温、代表性天气描述与图标,开发成本最低。
修改后的异步请求逻辑
async getForecast() { // 替换为逐日预报接口地址 let url = "https://api.openweathermap.org/data/2.5/forecast/daily"; let payload = { lat: this.$app.globals.origin.lat, lon: this.$app.globals.origin.lng, units: 'metric', APPID: '0feb06072d87320932559f321ca221fb', lang: 'es', // cnt=5表示直接返回未来连续5天的逐日聚合数据 cnt: 5 }; let response = await axios.get(url, { params: payload }); this.data = response.data.list; }
注意:逐日接口的字段结构与原3小时接口存在差异,原3小时接口的温度值挂载在
main对象下,逐日接口的温度值挂载在temp对象下,必须同步调整视图层的取值路径,否则会出现取值为空的问题。
同步调整后的Vue视图层代码
<div style="width:auto; height:100%; display:flex; align-items:center;"> <a style="width:auto; padding:10; height:100%; display:flex; flex-direction:column; align-items:center; justify-content:space-around;" v-for="(item,index) in data" :key="index"> <!-- 调整为逐日接口的温度取值路径 --> <span class="fs_smaller c_normal">{{ item.temp.min }} / {{ item.temp.max }}</span> <span class="fs_smaller c_normal">{{ item.weather[0].description }}</span> <img style="width:50px; height:auto;" :src="'https://openweathermap.org/img/w/'+item.weather[0].icon+'.png'"> </a> </div>
方案2:基于现有3小时粒度接口自行聚合数据
如果当前API密钥权限不支持调用逐日预报接口,可拉取全量未来5天的3小时粒度数据,自行按日期分组聚合出每日的最高/最低温,选取代表性时段的天气描述与图标用于展示。
修改后的异步请求逻辑
async getForecast() { let url = "https://api.openweathermap.org/data/2.5/forecast"; let payload = { lat: this.$app.globals.origin.lat, lon: this.$app.globals.origin.lng, units: 'metric', APPID: '0feb06072d87320932559f321ca221fb', lang: 'es' // 移除原cnt:5参数,拉取全量未来5天的3小时粒度数据 }; let response = await axios.get(url, { params: payload }); const rawForecastList = response.data.list; const dailyForecastMap = {}; rawForecastList.forEach(item => { // 以时间戳转换的日期字符串为分组key,归并同日的所有时段数据 const dateKey = new Date(item.dt * 1000).toDateString(); if (!dailyForecastMap[dateKey]) { dailyForecastMap[dateKey] = { tempMin: item.main.temp_min, tempMax: item.main.temp_max, weather: item.weather[0] }; return; } // 遍历同日期的所有时段,更新当日的最高/最低温 dailyForecastMap[dateKey].tempMin = Math.min(dailyForecastMap[dateKey].tempMin, item.main.temp_min); dailyForecastMap[dateKey].tempMax = Math.max(dailyForecastMap[dateKey].tempMax, item.main.temp_max); // 优先选取当日中午12点的天气条目作为展示用,参考性更强 const itemHour = new Date(item.dt * 1000).getHours(); if (itemHour === 12) { dailyForecastMap[dateKey].weather = item.weather[0]; } }); // 取聚合完成后前5天的数据赋值给视图层 this.data = Object.values(dailyForecastMap).slice(0, 5); }
同步调整后的Vue视图层代码
<div style="width:auto; height:100%; display:flex; align-items:center;"> <a style="width:auto; padding:10; height:100%; display:flex; flex-direction:column; align-items:center; justify-content:space-around;" v-for="(item,index) in data" :key="index"> <span class="fs_smaller c_normal">{{ item.tempMin }} / {{ item.tempMax }}</span> <span class="fs_smaller c_normal">{{ item.weather.description }}</span> <img style="width:50px; height:auto;" :src="'https://openweathermap.org/img/w/'+item.weather.icon+'.png'"> </a> </div>
内容的提问来源于stack exchange,提问作者gabogabans
相关产品推荐
相关产品推荐

