openweathermap api:从 5 天预报中获取 1 个时间段

问题描述 投票:0回答:1

我正在使用 openweathermap api 5 天/ 3 小时预报

我得到以下结果集。每天有多个结果。每3小时就有结果。这是获取完整 json 的完整 api 调用https://api.openweathermap.org/data/2.5/forecast?id=3362024&APPID=bbcf57969e78d1300a815765b7d587f0&units=metric。顺便说一下,api 密钥仅用于测试。

{
"cod": "200",
"message": 0.0034,
"cnt": 37,
"list": [
{
"dt": 1518512400,
"main": {
"temp": 23.79,
"temp_min": 21.5,
"temp_max": 23.79,
"pressure": 1021.76,
"sea_level": 1029.07,
"grnd_level": 1021.76,
"humidity": 74,
"temp_kf": 2.29
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-13 09:00:00"
},
{
"dt": 1518523200,
"main": {
"temp": 26.2,
"temp_min": 24.68,
"temp_max": 26.2,
"pressure": 1019.83,
"sea_level": 1027.02,
"grnd_level": 1019.83,
"humidity": 63,
"temp_kf": 1.52
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {},
"sys": {1 item},
"dt_txt": "2018-02-13 12:00:00"
},
{
"dt": 1518534000,
"main": {
"temp": 25.8,
"temp_min": 25.04,
"temp_max": 25.8,
"pressure": 1018.1,
"sea_level": 1025.39,
"grnd_level": 1018.1,
"humidity": 60,
"temp_kf": 0.76
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {},
"sys": {1 item},
"dt_txt": "2018-02-13 15:00:00"
},
{8 items},
{8 items},
{
"dt": 1518566400,
"main": {
"temp": 18.47,
"temp_min": 18.47,
"temp_max": 18.47,
"pressure": 1019.07,
"sea_level": 1026.25,
"grnd_level": 1019.07,
"humidity": 91,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 00:00:00"
},
{
"dt": 1518577200,
"main": {
"temp": 18.48,
"temp_min": 18.48,
"temp_max": 18.48,
"pressure": 1017.23,
"sea_level": 1024.63,
"grnd_level": 1017.23,
"humidity": 91,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 03:00:00"
},
{
"dt": 1518588000,
"main": {
"temp": 18.11,
"temp_min": 18.11,
"temp_max": 18.11,
"pressure": 1018.54,
"sea_level": 1025.84,
"grnd_level": 1018.54,
"humidity": 94,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 06:00:00"
},
{
"dt": 1518598800,
"main": {
"temp": 19.59,
"temp_min": 19.59,
"temp_max": 19.59,
"pressure": 1018.87,
"sea_level": 1026.14,
"grnd_level": 1018.87,
"humidity": 87,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {},
"sys": {1 item},
"dt_txt": "2018-02-14 09:00:00"
},
{
"dt": 1518609600,
"main": {
"temp": 21.38,
"temp_min": 21.38,
"temp_max": 21.38,
"pressure": 1017.75,
"sea_level": 1024.92,
"grnd_level": 1017.75,
"humidity": 80,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 12:00:00"
},
{
"dt": 1518620400,
"main": {
"temp": 20.64,
"temp_min": 20.64,
"temp_max": 20.64,
"pressure": 1017.45,
"sea_level": 1024.32,
"grnd_level": 1017.45,
"humidity": 83,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 15:00:00"
},
{
"dt": 1518631200,
"main": {
"temp": 19.29,
"temp_min": 19.29,
"temp_max": 19.29,
"pressure": 1017.61,
"sea_level": 1024.57,
"grnd_level": 1017.61,
"humidity": 90,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 18:00:00"
},
{
"dt": 1518642000,
"main": {
"temp": 18.93,
"temp_min": 18.93,
"temp_max": 18.93,
"pressure": 1018.43,
"sea_level": 1025.58,
"grnd_level": 1018.43,
"humidity": 90,
"temp_kf": 0
},
"weather": [1 item],
"clouds": {1 item},
"wind": {2 items},
"rain": {1 item},
"sys": {1 item},
"dt_txt": "2018-02-14 21:00:00"
}
],
"city": {
"id": 3362024,
"name": "Rosebank",
"coord": {
"lat": -33.9556,
"lon": 18.4742
},
"country": "ZA"
}
}

我的问题是,我只需要每天的“12:00”时间段,而数据返回时每天有多个时间段。

我已尝试以下方法,但效果不佳

  getData(){
    var arraycontainsMidday;
    this.http.get('https://api.openweathermap.org/data/2.5/forecast?id=3362024&APPID=bbcf57969e78d1300a815765b7d587f0&units=metric').map(res=>res.json()).subscribe(data => {
    this.items = data;
      for(var i = 0; i < this.items.list.length; i++){
        this.dates = this.items.list[i].dt_txt.substring(10);
        this.temps = this.items.list[i].main;
        //if 12 o'clock middat is found
        arraycontainsMidday = (this.dates.indexOf("12:00:00") > -1);
      }

      for(var j = 0; j < this.items.list.length; j++){
        this.temps = this.items.list[j].main.temp;
      }

    })
  }
javascript arrays json object openweathermap
1个回答
0
投票

我也遇到了同样的问题。我找到了这个解决方案。

const groupByDay = (data) => {
    const groupData = {}
    data.forEach((item) => {
        const date = item.dt_txt.split(' ')[0]
        if (!groupData[date]) {
            groupData[date] = item;
        }
    })
    return Object.values(groupData)
}

在设置数据之前使用此功能。

© www.soinside.com 2019 - 2024. All rights reserved.