解析json错误

时间:2013-01-14 03:04:41

标签: javascript json parsing

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

1 个答案:

答案 0 :(得分: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>