我有JSON文件(staff.json):
[{
"Position" : "Programmer",
"Name" : "Giacomo Gulizzoni",
"Age" : 37,
"Sex" : "Male",
"Project" : "SmartFactory"
}, {
"Position" : "Tester",
"Name" : "Marko Botton",
"Age" : 34,
"Sex" : "Male",
"Project" : "SmartFactory"
}, {
"Position" : "Tester",
"Name" : "Markn",
"Age" : 60,
"Sex" : "Male",
"Project" : "SmartFactory"
}, {
"Position" : "Tester",
"Name" : "Mariah Maclachian",
"Age" : 37,
"Sex" : "Female",
"Project" : "SmartFactory"
}, {
"Position" : "Tester",
"Name" : "Valerie Liberty",
"Age" : 25,
"Sex" : "Female",
"Project" : "SmartProject"
}, {
"Position" : "Programmer",
"Name " : "Guido Jack Gulizzoni",
"Age" : 22,
"Sex" : "Male",
"Project" : "SmartProject"
}, {
"Position" : "Programmer",
"Name " : "Guioni",
"Age" : 44,
"Sex" : "Male",
"Project" : "SmartProject"
}]
这段代码(table-generator.js)从这个json生成一个表:
define('table-generator', function () {
var xmlhttp = new XMLHttpRequest();
var url = "js/json/staff.json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table><tr><th>Position</th><th>Name</th><th>Age</th><th>Sex</th><th>Project</th></tr>";
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].Position +
"</td><td>" +
arr[i].Name +
"</td><td>" +
arr[i].Age +
"</td><td>" +
arr[i].Sex +
"</td><td>" +
arr[i].Project +
"</td></tr>";
}
out += "</table>";
document.getElementById("id01").innerHTML = out;
}
});
问题:当我向JSON文件中添加更多人时,他们在表中的名称将生成为Undefined。只有名称,其他数据才能正确显示。无法找出,原因是什么(可能是require.js)。 有人可以帮帮我吗? 页面链接位于以下评论中。
答案 0 :(得分:6)
数组中的最后两个对象的名称键为&#34; Name&#34; (末尾有空格)而不是&#34;姓名&#34;。