如何使用此API返回的温度是多少?使用JavaScript? [英] How to use this api to return the temperature? With JavaScript?

查看:235
本文介绍了如何使用此API返回的温度是多少?使用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我给这个code和要建立在其上,这样的温度也回来了。

Hi I was given this code and want to build upon it so that the temperature is also returned.

我使用OpenWeatherMap API。这里是code,我需要拉从信息

I am using 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.

虽然当我尝试按照这个更增添了code它打破了,并且没有返回。

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

即VAR温度...但下次来,我不能让我的头左右。

i.e var temp... but what come next I can't get my head around.

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";

然后当这是固定的,我则需要改变温度,使得它本身显示我已成立了一个div里面呢?

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?

任何帮助是AP preciated,

Any help is appreciated,

感谢,

扎克

推荐答案

下面就是我会做的。

// 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")

这篇关于如何使用此API返回的温度是多少?使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆