【发布时间】:2020-01-23 01:18:32
【问题描述】:
我使用的示例来自:
Get JSON data from external URL and display it in a div as plain text
从带有嵌套数据的 JSON 中获取信息,但我得到了结果:
[object Object]
如何获取姓名或职位信息?
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"
}
}
]
}
【问题讨论】:
标签: javascript json