47

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

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

{
    //...
    "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.

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

Does anyone know what temperature unit openweathermap is returning?

4

6 回答 6

147

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

查看API 文档,如果您添加&units=metric到您的请求中,您将获得摄氏度。

于 2013-10-20T12:34:10.770 回答
14

这似乎是开尔文,但您可以指定要为 temp 返回的格式,例如:

http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=metric

或者

http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=imperial

于 2013-10-20T12:36:29.047 回答
4

开尔文到华氏度是:

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

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

于 2016-10-05T19:42:00.340 回答
1

首先确定您想要哪种格式。在您的 BASE_URL 中发送城市后,仅添加&mode=json&units=metric 。您将从服务器获得直接的摄氏温度值。

于 2020-10-02T13:16:21.627 回答
1

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

这是我的代码。

<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>
于 2017-11-15T15:01:51.113 回答
1

试试这个例子

curl --location --request GET 'http://api.openweathermap.org/data/2.5/weather?q=Manaus,br&APPID=your_api_key&lang=PT&units=metric'
于 2021-10-26T18:00:53.550 回答