1

我正在尝试使用 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`
4

1 回答 1

0

这似乎有效。

jsFiddle

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>
于 2013-01-14T03:10:56.367 回答