23

Sto ottenendo il tempo per una città usando openweathermap.org.Come si calcola la temperatura in Celsius restituita in openweathermap.org JSON?

La chiamata jsonp funziona e tutto va bene, ma l'oggetto risultante contiene la temperatura in un'unità sconosciuta:

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

Ecco una demo che console.log 's l'oggetto completo.

Non penso che la temperatura risultante sia in gradi Fahrenheit perché la conversione da 290.38 fahrenheit a celsius è 143.544.

Qualcuno sa quale unità di temperatura openweathermap sta restituendo?

risposta

75

Sembra kelvin. Convertire kelvin in celsius è facile: basta sottrarre 273.15.

E il brevissimo sguardo a the API documentation ci dice che se si aggiunge &units=metric alla vostra richiesta, si otterrà indietro Celsius.

+0

@TJCrowder Non è un'impostazione predefinita leggermente strana? – hitautodestruct

+3

@hitautodestruct: È per * me *, ma poi, non sono uno scienziato. :-) –

+4

Kelvin (http://en.wikipedia.org/wiki/Kelvin) è l'unità di temperatura del "Sistema internazionale di unità". È assoluto, basato sulla fisica. È zero è lo "zero assoluto". Sembra una scelta del tutto naturale per un "default", per me ... – MarcoS

6

che sembra essere kelvin, ma è possibile specificare il formato si desidera venga restituito per la temperatura, ad esempio:

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

o

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

+0

Grazie per aver trovato il tempo di rispondere! – hitautodestruct

+0

@spacebean Mostra errore come questo: '{" cod ": 401," message ":" Chiave API non valida. Per ulteriori informazioni, vedere http://openweathermap.org/faq#error401 "}' –

1

È possibile modificare l'unità in metrica.

Questo è il mio codice.

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