In: Computer Science
I want to know how to get the "temp" information from a API response like this in javascript:
{
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 350
},
"clouds": {
"all": 1
},
"dt": 1560350645,
"sys": {
"type": 1,
"id": 5122,
"message": 0.0139,
"country": "US",
"sunrise": 1560343627,
"sunset": 1560396563
},
"timezone": -25200,
"id": 420006353,
"name": "Mountain View",
"cod": 200
}
let Temperature = undefined;
let Wind = undefined;
let Humid = undefined;
let Visibility = undefined;
let weatherInfo = undefined;
I've written those things in my js work and I want to get temp, wind speed, humid and visibility in the api response.:
function getWeatherInfo (latitude, longitude) {
weatherInfo = "api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&appid={API key}";
return weatherInfo;
}
is my API right?
const fetch = require('node-fetch');
/////////////////////////////////////////////////////////////////
let lat = 37.39;
let lon = -122.08;
let key = "bff2d6d58917bc7f9587c06c3e5fc63e"
async function getapi(lat, lon, key) {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${String(lat)}&lon=${String(lon)}&appid=${String(key)}`;
const response = await fetch(url);
return await response.json();
}
(async () => {
const arr = await getapi(lat, lon, key);
console.log(arr);
console.log("Temperature is ", arr.main.temp);
console.log("Pressure is ", arr.main.pressure);
console.log("Humidity is ", arr.main.humidity);
console.log("Visibility is ", arr.visibility);
console.log("Wind is is ", arr.wind.speed);
console.log("Weather description is ", arr.weather[0].description)
})()

IF YOU HAVE ANY DOUBT THEN JUST LEAVE A COMMENT AND I WILL CONNECT WITH YOU AS SOON AS POSSIBLE