0
這是我第一次使用JSON。我有一個外部json文件,我在我的html文件中使用json文件來創建每個json數據讀取的div。我可以得到所有值的正確輸出,除了'描述'值之外。我曾嘗試將它打印到cosole日誌中,但是我爲特定值獲取的所有內容都是'未定義的',但其他值是正確的。任何想法爲什麼發生這種情況?'未定義'JSON數據
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文件中存在錯誤,因爲雙引號位於冒號後面。它應該是「描述」:「測試」 –
@DavisMolinari抱歉,這不是原始代碼的一部分。這個問題仍然存在 – nurul98
@ nurul98我不知道,我認爲它的JSON字符串。試着用'list = JSON.parse(list)'在成功函數第一行 – prasanth