如何计算 openweathermap.org JSON 中返回的摄氏度温度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19477324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:55:45  来源:igfitidea点击:

How do I calculate the temperature in celsius returned in openweathermap.org JSON?

javascriptjsonunits-of-measurementweather-apiopenweathermap

提问by hitautodestruct

I'm getting the weather for a city using openweathermap.org.

我正在使用 openweathermap.org 获取城市的天气。

The jsonp call is working and everything is fine but the resulting object contains the temperature in an unknown unit:

jsonp 调用正常工作,一切正常,但生成的对象包含未知单位的温度:

{
    //...
    "main": {
        "temp": 290.38, // What unit of measurement is this?
        "pressure": 1005,
        "humidity": 72,
        "temp_min": 289.25,
        "temp_max": 291.85
    },
    //...
}

Here is a demo that console.log's the full object.

这是console.log完整对象的演示。

I don't think the resulting temperature is in fahrenheit because converting 290.38fahrenheit to celsius is 143.544.

我不认为由此产生的温度是以290.38华氏度为单位的,因为将华氏度转换为摄氏度是143.544.

Does anyone know what temperature unit openweathermap is returning?

有谁知道 openweathermap 返回的是什么温度单位?

回答by T.J. Crowder

It looks like kelvin. Converting kelvin to celsius is easy: Just subtract 273.15.

它看起来像开尔文。将开尔文转换为摄氏度很容易:只需减去 273.15。

And the briefest glanceat the API documentationtells us that if you add &units=metricto your request, you'll get back celsius.

简短的一瞥,在API文档告诉我们,如果你添加&units=metric到你的要求,你会回来摄氏度。

回答by Sara

Kelvin to Fahrenheit is:

开尔文到华氏度是:

(( kelvinValue - 273.15) * 9/5) + 32

I've noticed not all of the OpenWeatherApp calls read the units parameter if its passed in. (An example of this error: http://api.openweathermap.org/data/2.5/group?units=Imperial&id=5375480,4737316,4164138,5099133,4666102,5391811,5809844,5016108,4400860,4957280&appid=XXXXXX) Kelvin is still returned.

我注意到并非所有 OpenWeatherApp 调用都会读取传入的单位参数。(此错误的示例:http: //api.openweathermap.org/data/2.5/group?units=Imperial&id=5375480,4737316 , 4164138,5099133,4666102,5391811,5809844,5016108,4400860,4957280&appid=XXXXXX) 开尔文仍然返回。

回答by Darragh Blake

You can change the unit to metric.

您可以将单位更改为公制。

This is my code.

这是我的代码。

<head>
    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
        <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
        <style type="text/css">]
        body{
            font-size: 100px;

        }

        #weatherLocation{

            font-size: 40px;
        }
        </style>
        </head>
        <body>
<div id="weatherLocation">Click for weather</div>

<div id="location"><input type="text" name="location"></div>

<div class="showHumidity"></div>

<div class="showTemp"></div>

<script type="text/javascript">
$(document).ready(function() {
  $('#weatherLocation').click(function() {
    var city = $('input:text').val();
    let request = new XMLHttpRequest();
    let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=[YOUR API KEY HERE]`;


    request.onreadystatechange = function() {
      if (this.readyState === 4 && this.status === 200) {
        let response = JSON.parse(this.responseText);
        getElements(response);
      }
    }

    request.open("GET", url, true);
    request.send();

    getElements = function(response) {
      $('.showHumidity').text(`The humidity in ${city} is ${response.main.humidity}%`);
      $('.showTemp').text(`The temperature in Celcius is ${response.main.temp} degrees.`);
    }
  });
});
</script>

</body>