Javascript 如何计算openweathermap.org JSON返回的摄氏度温度?
我正在使用openweathermap.org获取一个城市的天气 jsonp调用正在工作,一切正常,但结果对象包含未知单位的温度:Javascript 如何计算openweathermap.org JSON返回的摄氏度温度?,javascript,json,units-of-measurement,weather-api,openweathermap,Javascript,Json,Units Of Measurement,Weather Api,Openweathermap,我正在使用openweathermap.org获取一个城市的天气 jsonp调用正在工作,一切正常,但结果对象包含未知单位的温度: { //... "main": { "temp": 290.38, // What unit of measurement is this? "pressure": 1005, "humidity": 72, "temp_min": 289.25, "temp_max":
{
//...
"main": {
"temp": 290.38, // What unit of measurement is this?
"pressure": 1005,
"humidity": 72,
"temp_min": 289.25,
"temp_max": 291.85
},
//...
}
下面是一个演示,console.log
是完整的对象
我不认为产生的温度是华氏温度,因为将华氏温度转换成摄氏温度是143.544
有人知道openweathermap返回的温度单位是多少吗?。将开尔文转换为摄氏度很容易:只需减去273.15
最简单的一瞥告诉我们,如果您将
&units=metric
添加到请求中,您将返回摄氏度。看起来是开尔文,但您可以指定要返回的温度格式,例如:
或
开尔文到华氏度是:
(( kelvinValue - 273.15) * 9/5) + 32
我注意到,如果传入units参数,并非所有OpenWeatherApp调用都会读取它。
(此错误的一个示例:
)
开尔文仍然返回。您可以将单位更改为公制 这是我的密码
<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>
]
身体{
字体大小:100px;
}
#气象定位{
字体大小:40px;
}
点击查看天气
$(文档).ready(函数(){
$(“#weatherLocation”)。单击(函数(){
var city=$('input:text').val();
let request=new XMLHttpRequest();
让url=`http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=[此处的API键]`;
request.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
让response=JSON.parse(this.responseText);
获取元素(响应);
}
}
打开(“获取”,url,true);
request.send();
getElements=函数(响应){
$('.show湿度').text(`city}中的湿度为${response.main.湿度}%`);
$('.showTemp').text(`Celcius中的温度为${response.main.temp}度。`);
}
});
});
首先确定您想要哪种格式。
在基本URL中发送city后,仅添加&mode=json&units=metric。您将从服务器获得直接摄氏度值。@TJCrowder这不是一个有点奇怪的默认值吗?@hitautodestruct:对我来说是这样,但我不是科学家。:-)开尔文()是“国际单位制”中的温度单位。它是绝对的,基于物理学。它的零是“绝对零”。在我看来,这是一个相当自然的“默认”选择…@MarcoS:当然,但这是天气信息。99.9999999%使用天气信息的人会使用摄氏度或华氏度,甚至(我愿意打赌)绝大多数气象学家也会使用。默认值适用于普通情况,而不是百万情况下的默认值。:-)我的英雄走了。看着他走。甚至没有看到&units文档。感谢您抽出时间回答@spacebean显示如下错误:
{“cod”:401,“消息”:“无效的API密钥。请参阅http://openweathermap.org/faq#error401 有关更多信息,请参见“}
如果您将&lat=…&lng=…&units=metric与一起传递,则不再有效。