You are trying to access a property within an object so the syntax you used is not correct, the correct one using the square brackets would be ["main"]["temp"]
, that is, in each pair of brackets a property of the object as commented by Augusto Vasques, i particularly do not like to use this notation, I prefer to access properties of objects with the sign of dot (.) by making the code more readable.
Imagine if you had to access a property in the index 0 for example the syntax would look like this: respostajson["main"][0]["temp"]
, for me it gets strange and more confusing than the notation by point: respostajson.main[0].temp
Then you can do as commented using brackets or as in the example below:
var xhr = new XMLHttpRequest();
var bot = document.querySelector(".botao");
bot.addEventListener("click", function() {
console.log('buscando...')
xhr.open("GET", "https://api.openweathermap.org/data/2.5/weather?id=3450554&appid=6b8661b06c381528f71386ee3a01ff53&units=metric")
xhr.send();
})
xhr.addEventListener("load", function() {
var resposta = xhr.responseText;
var respostajson = JSON.parse(resposta);
console.log(respostajson)
console.log(respostajson.main.temp)
});
<button class="botao">Buscar</button>
Do it like this
console.log(respostajson["main"]["temp"])
– Augusto Vasques