簡體   English   中英

無法從Openweathermap API獲取溫度

[英]Can't get temperature from Openweathermap API

我正在嘗試從以下JSON中獲取temp變量:

"weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        },
],
"main": {
        "temp": 273.15,
        "pressure": 992,
        "humidity": 97,
        "temp_min": 273.15,
        "temp_max": 273.15
    },

我可以使用JavaScript獲得天氣價值:

 function print(xxx){
  var value = "";
    for(var i = 0;i < result.weather.length;i++){
    value += result.weather[i].main "<br/>";
    }

如果我嘗試這樣獲得溫度:

function print(xxx){
  var value = "";
    for(var i = 0;i < result.main.length;i++){
    value += result.main[i].temp "<br/>";
    }

我什么也沒得到。 沒有錯誤或值。

不同之處在於您要查看的數據類型。 weather是一個數組。 這就是為什么要使用for循環遍歷數組中的每個項目的原因。

main只是一個對象。 沒有什么可以重復的。 它甚至沒有length屬性。

因此訪問main ,將完全忘記for循環。 只需直接訪問即可。

value += result.main.temp + "<br />";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM