我想从json数据显示所有带有XMLHttpRequest的Jobs,但是我无法显示我的数据。这是我的功能:
function getJobs () {
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'http://codepen.io/jobs.json', true);
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4) {
let data = JSON.parse(httpRequest.responseText);
Object.keys(data).forEach( (key) => {
console.log(key[0].company_name); // undefine
});
}
};
httpRequest.send();
}
getJobs();
你能帮我解决一下我的forEach吗? json是数组的对象,有一些对象^^
谢谢!
答案 0 :(得分:1)
key
变量包含属性的名称(即密钥),它不会保留属性的值。< / p>
var value = data[key];
console.log(value[0].company_name);
答案 1 :(得分:0)
试试这个
function getJobs () {
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'http://codepen.io/jobs.json', true);
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4) {
let data = JSON.parse(httpRequest.responseText);
data.jobs.forEach(function(key, value) {
console.log(key.company_name);
});
}
};
httpRequest.send();
}