这是我第一次使用 JSON。我有一个外部 json 文件,并且我在 html 文件中使用 json 文件来创建包含读取的每个 json 数据的 div。我可以获得所有值的正确输出,除了“描述”值之外。我尝试将其打印到控制台日志,但我得到的该特定值是“未定义”,但其他值都可以。知道为什么会发生这种情况吗?
JSON 文件:
[{"title":"3G","filePath":"https://example.com","descript":"hello world"}, {"title":"4G", "filePath":"https://example.com", "descript": "test"} ]
HTML 文件:
$.ajax({
url : "testJSON.json",
type : "get", // whichever you like
contentType:"json",
success : function(list)
{
var divCol = "<div class='col-sm-4 col-md-4'>";
var divWell = "<div class='well'>";
var divClose= "</div>";
list.forEach(function(obj, index) {
var title = "<h5>" + obj.title + "</h5>";
var desc = "<p>" + obj.descript + "</p>";
var linkStart = "<a class='btn btn-default' style='float:left' href='" + obj.filePath + "' target='_blank'>";
var linkEnd = "CSV</a>";
var div = divCol +
divWell +
title +
desc +
linkStart +
// image +
linkEnd +
divClose +
divClose;
console.log(list)
$("#imdaFiles").append(div); // insert the div you've just created
})
}
});
最佳答案
一个简短的通知可能会解决该问题..您在 JSON 中最后一个 descript
键的右侧放错了双引号。
关于javascript - “未定义”JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383613/