我正在尝试使用javascript eval()函数解析以下JSON。
`[{"pk": 1, "model": "relayapp.team", "fields": {"company": 1, "signup": true, "name": "Cool"}}]`
function setupTable(data)
{
var info = eval("(" + data + ")");
var string = "";
for(item in info.fields)
{
record = info.fields[item];
string = string + "<tr><td>" + record.name + "</td><td> </td><td> " + record.signup + "</td><td></td><td> </td></tr>";
}
document.getElementById("team_table_body").innerHTML = string;
}`
它在控制台调试器中出现此错误。
`Uncaught SyntaxError: Unexpected identifier teamList.html:54
setupTable teamList.html:54
(anonymous function) teamList.html:48
l jquery.min.js:2
c.fireWith jquery.min.js:2
T jquery.min.js:2
r jquery.min.js:2`
答案 0 :(得分:0)
这似乎有效。
<强> JS 强>
function setupTable(data)
{
var info = eval("(" + data + ")");
var string = "";
for(var i = 0; i < info.length; i++){
record = info[i];
string = string + "<tr><td>" + record.fields.name + "</td><td> </td><td> " + record.fields.signup + "</td><td></td><td> </td></tr>";
}
document.getElementById("team_table_body").innerHTML = string;
}
setupTable('[{"pk": 1, "model": "relayapp.team", "fields": {"company": 1, "signup": true, "name": "Cool"}}]');
<强> HTML 强>
<table id='team_table_body'></table>