Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从OpenWeatherMap API获取特定字段时遇到问题';s输出,通过JS/XMLHttpRequest_Javascript_Concatenation_Openweathermap - Fatal编程技术网

Javascript 从OpenWeatherMap API获取特定字段时遇到问题';s输出,通过JS/XMLHttpRequest

Javascript 从OpenWeatherMap API获取特定字段时遇到问题';s输出,通过JS/XMLHttpRequest,javascript,concatenation,openweathermap,Javascript,Concatenation,Openweathermap,因此,我正在使用OpenWeatherMapAPI构建这个web forecast应用程序,到目前为止,我能够从列表输出的第一次迭代中获取数据,但是我还需要获取其他特定字段的数据。以下是我的一些代码: ajaxGet("https://api.openweathermap.org/data/2.5/onecall?lat=4.6097&lon=-74.0817&exclude=current,minutely,hourly,alerts&appid=APPID&

因此,我正在使用OpenWeatherMapAPI构建这个web forecast应用程序,到目前为止,我能够从列表输出的第一次迭代中获取数据,但是我还需要获取其他特定字段的数据。以下是我的一些代码:

ajaxGet("https://api.openweathermap.org/data/2.5/onecall?lat=4.6097&lon=-74.0817&exclude=current,minutely,hourly,alerts&appid=APPID&units=metric", function (response) {

      var data = JSON.parse(response);
      console.log(data);
      
      var temperature = document.createElement("h6");
      temperature.textContent = data.daily[0].temp.max + "°" + " / " + data.daily[0].temp.min + "°";

      document.getElementById("temperaturaBogVier").appendChild(temperature);
  });
下面是一个API输出的示例(我在这里只展示了第一次迭代,但总共至少有6次)

如您所见,我可以将“data.daily[0].temp.”中包含的数据打印到HTML中,但它只适用于第一组字段,我不知道如何选择特定的迭代。我肯定我在concat中遗漏了一些东西,但到目前为止,我所尝试的都没有奏效


任何帮助都将受到极大的感谢,并会得到一份想象中的华夫饼干。THX:D

每天的温度
数据。daily
定义为对象的JavaScript数组。您只需通过它们的索引来访问它们,该索引指示它们在数组中的位置

data.daily[0] // First element
data.daily[1] // Second element
data.daily[2] // Third element
一旦在数组中选择了一个对象,就可以访问某些值,如
data.daily[2].temp.max

数组最酷的一点是,您可以使用循环来迭代它们。如果您想打印出每天的温度,这将为您节省大量的文字:

ajaxGet("https://api.openweathermap.org/data/2.5/onecall?lat=4.6097&lon=-74.0817&exclude=current,minutely,hourly,alerts&appid=YOUR_API_KEY_HERE&units=metric", function (response) {

  var data = JSON.parse(response);
  console.log(data);

  data.daily.forEach(function (date) {
    var temperature = document.createElement("h6");
    temperature.textContent = date.temp.max + "°" + " / " + date.temp.min + "°";

    document.getElementById("temperaturaBogVier").appendChild(temperature);
  })
});

请注意:我已删除请求URL的
appid=XXXXX
部分,因为它包含您的OpenWeather个人API密钥,您不应公开共享该密钥。

如果我理解正确,您希望获取所有每日最大/最小值,并将它们放入要附加到另一个元素的元素中

这里有一个方法可以做到这一点

ajaxGet("https://api.openweathermap.org/data/2.5/onecall?lat=4.6097&lon=-74.0817&exclude=current,minutely,hourly,alerts&units=metric", function (response) {
    var data = JSON.parse(response);
    console.log(data);

    data.daily
        .map(datapoint => datapoint.temp) //get the temp value/object from each datapoint
        .map(temp => { //create an element and add each max/min value to that element's textContent
            const temperature = document.createElement("h6");
            temperature.textContent = temp.max + "° / " + temp.min + "°";
            return temperature;
        })
        .forEach(element => { //add each of the elements created in the previous map to the desired element
            document.getElementById("temperaturaBogVier").appendChild(element);
        });
});

正如在另一个答案中指出的那样,我也删除了app id查询参数

你的意思是像
data.daily[1]
data.daily[2]
等吗。?或者,对于特定的迭代,您的意思是什么?@DMalan嗯,输出在data.daily[0]中有一个数组,它只包含一个列表,因此没有数据。daily[1],data.daily[2]等等。。我试图抓住这个列表中包含的元素,但由于它本身不是数组,我不太确定如何做到这一点。。。TY为您的答复BTWY可以考虑删除或替换<代码> AppID= XXXX < /代码>请求URL的一部分。这是OpenWeather的个人API密钥,您不应该共享。@DiegoP<代码>数据。每日[0]是一个对象和
数据。每日
是一个数组,您可以使用
数据进行索引。每日[1]
数据。每日[2]
等。@DMalan谢谢,您完全正确。我只打了一行电话,所以我无法获取其余信息。干杯非常感谢你的解释,我错误地处理了数组。它工作得很好!
ajaxGet("https://api.openweathermap.org/data/2.5/onecall?lat=4.6097&lon=-74.0817&exclude=current,minutely,hourly,alerts&units=metric", function (response) {
    var data = JSON.parse(response);
    console.log(data);

    data.daily
        .map(datapoint => datapoint.temp) //get the temp value/object from each datapoint
        .map(temp => { //create an element and add each max/min value to that element's textContent
            const temperature = document.createElement("h6");
            temperature.textContent = temp.max + "° / " + temp.min + "°";
            return temperature;
        })
        .forEach(element => { //add each of the elements created in the previous map to the desired element
            document.getElementById("temperaturaBogVier").appendChild(element);
        });
});