例如,
for (var i = 0; i < 5; i ++)
{
console.log(jsonfile[i].Username)
}我尝试了一下,得到了如下结果:
TypeError: Cannot read property 'Username' of undefined我的JSON是:
[
{
"Username": "ozziep",
"ProjectID": "ExpressJS",
"TimeStamp": "2016-12-30T19:54:52.418Z",
"Comments": "hello world how are we today?"
}
]我正在使用require加载JSON。
发布于 2017-01-03 01:30:30
由于您的jsonfile中只有一个对象,因此jsonFile[1]将是未定义的。
使用如下所示的i < jsonfile.length代替i < 5:
for (var i = 0; i < jsonfile.length; i ++)
{
console.log(jsonfile[i].Username)
}发布于 2017-01-03 02:50:20
这确实起到了作用,但还是要感谢大家的投入。
for( var user in jsonfile) {
console.log(jsonfile[user].Username);
console.log("------------------------")
console.log(jsonfile[user].Comments);
console.log("------------------------")
console.log(jsonfile[user].ProjectID);
console.log("------------------------")
console.log("")}
https://stackoverflow.com/questions/41430918
复制相似问题