简体   繁体   English

如何使用此api返回温度? 使用JavaScript?

[英]How to use this api to return the temperature? With JavaScript?

Hi I was given this code and want to build upon it so that the temperature is also returned. 嗨,我收到了此代码,并希望在此代码的基础上进行构建,以便还返回温度。

I am using OpenWeatherMap API. 我正在使用OpenWeatherMap API。 Here is the code that I need to pull the information from 这是我需要从中获取信息的代码

{  
   "coord":{  
      "lon":98.86,
      "lat":55.08
   },
   "weather":[  
      {  
         "id":801,
         "main":"Clouds",
         "description":"few clouds",
         "icon":"02n"
      }
   ],
   "base":"cmc stations",
   "main":{  
      "temp":245.781,
      "pressure":969.38,
      "humidity":48,
      "temp_min":245.781,
      "temp_max":245.781,
      "sea_level":1050.78,
      "grnd_level":969.38
   },
   "wind":{  
      "speed":1.07,
      "deg":138.501
   },
   "clouds":{  
      "all":12
   },
   "dt":1450824930,
   "sys":{  
      "message":0.0075,
      "country":"RU",
      "sunrise":1450748926,
      "sunset":1450774674
   },
   "id":1488616,
   "name":"Uk",
   "cod":200
}

I have put in bold what I think I need to use. 我以粗体显示了我认为需要使用的内容。

As I am already calling out some information as you can see below. 正如我已经在呼唤一些信息,如下所示。

Though when I try to follow this and add more code it breaks and returns nothing. 虽然当我尝试遵循此步骤并添加更多代码时,它中断并没有返回任何内容。

I think it is here. 我认为就在这里。 I might even need to make a new var? 我什至需要创建一个新的变量? for it 为了它

ie var temp... but what come next I can't get my head around. 就是说var temp ...但是接下来我将无法解决。

var weather = json.weather[0].main
                setIconAndDescription(weather, location)

            }
            else {


                description = "Oops, I couldn't find the weather in " + location;
                document.getElementById("description").innerHTML = description;

            }
        }
    }
}

function setIconAndDescription(weather, location){

    var icon;
    var description;


    weather = weather.toLowerCase();


    if (weather == "clear sky"
        || weather == "clear"){

        icon = "clear.svg";
        description = "Yay, sunshine!";
        document.body.style.backgroundColor = "#FA6144";
        document.getElementById("icon").style.backgroundColor = "#7A2F21";
            document.getElementById("temp").style.backgroundColor = "#7A2F21";
        document.getElementById("description").style.backgroundColor = "#E0563D";

Then when this is fixed I am then need to change the temperature so that it displays itself inside a div I have set up? 然后,当此问题解决后,我需要更改温度,以便将其显示在我已设置的div内?

Any help is appreciated, 任何帮助表示赞赏,

thanks, 谢谢,

Zack 扎克

Here's how I'd do it. 这就是我的做法。

// Get the temperature
var temp = json.main.temp

// Get your div you want to put it in
var mydiv = document.getElementById('mydivid')

// Set the inner HTML as that temperature (plus units, in your case, kelvin I guess)
mydiv.innerHTML(temp + "K")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM