I am using the example from:
Get JSON data from external URL and display it in a div as plain text
To get information from a JSON with nested data but I am getting the result:
[object Object]
How can I get the NAME or JOB information?
HTML
<div id="result" style="color:red"></div>
JavaScript
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON('https://www.googleapis.com/freebase/v1/text/en/bob_dylan').then(function(data) {
alert('Your Json result is: ' + data.queries); //you can comment this, i used it to debug
result.innerText = data.queries; //display the result in an HTML element
}, function(status) { //error detection....
alert('Something went wrong.');
});
JSON
{
"queries":[
{
"query":{
"CODE":"555443567",
"NAME":"NAME LASTNAME",
"JOB":"JOB TITLE"
}
}
]
}