我收到方括号的语法错误' ['来自我的JSON文件。以下是JSON。
[
{
"Product Name":"CPE 10281",
"Application":"Tyres",
"Weight":150,
"Cost to Produce":5000,
"Grade":"A",
"Cost to Sell":40000,
"Strength":100
},
{
"Product Name":"CPE 10282",
"Application":"computers",
"Weight":250,
"Cost to Produce":4000,
"Grade":"H",
"Cost to Sell":25000,
"Strength":90
}
]
我正在尝试使用AJAX来读取我的JSON文件。
$.ajax({
url: "dataProductJSON.json",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val) {
item.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},
});
我使用Apache Geronimo作为服务器从Eclipse运行我的html。 请帮忙。
答案 0 :(得分:2)
您错过了以下一行中的{
success: function (data)
成功
success: function (data) {
修改强>
您正在正确解析数据,请按以下方式执行
$.ajax({
url: "test.html",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val){
$.each(val, function (innerKey, innerValue){
item.push('<li id="' + innerKey + '">' + innerValue + '</li>');
});
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},
});
你需要使用2个循环,一个用于数组,另一个循环遍历对象属性
我尝试了一切,但工作正常