I keep getting undefined in the DOM...
I am trying to extract the city and state from this and display it in the DOM.
{
"post code": "90210",
"country": "United States",
"country abbreviation": "US",
"places": [
{"place name": "Beverly Hills",
"longitude": "-118.4065",
"state": "California",
"state abbreviation": "CA",
"latitude": "34.0901"}
]
}
var data = JSON.parse(ajax.responseText);
//var str = '';
var boo = '';
//str = data[1];
boo += data.state;
//document.getElementById('city').innerText = str;
document.getElementById('state').innerHTML = boo;
} else { // Status error!
//document.getElementById('city').innerHTML = ajax.statusText;
document.getElementById('state').innerHTML = ajax.statusText;
}
It looks like that 'state' field is nested in another object which is nested inside of an array, so the correct way to access it would be
data.places[0].state