从Ajax源生成DataTables时JSON格式不匹配

时间:2013-03-12 00:13:09

标签: json datatables

我希望生成一个带有ajax响应的DataTable作为源代码。我使用GSON库将员工的ArrayList转换为JSON字符串。我得到的JSON字符串是

{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
    {
        "id": 1,
        "firstName": "darsheet",
        "lastName": "shah",
        "city": "san jose",
        "state": "ca",
        "zip": 95112
    },
    {
        "id": 2,
        "firstName": "akshat",
        "lastName": "shah",
        "city": "ahmedabad",
        "state": "gj",
        "zip": 380061
    }
]

}

但是sAjaxSource属性需要以下格式的JSON字符串

{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
    {
        "0": 1,
        "1": "darsheet",
        "2": "shah",
        "3": "san jose",
        "4": "ca",
        "5": 95112
    },
    {
        "0": 2,
        "1": "akshat",
        "2": "shah",
        "3": "ahmedabad",
        "4": "gj",
        "5": 380061
    }
]

}

我使用的数据表代码是

$(document).ready(function(){
$('#refresh').click(function(){
    $('#emp').dataTable({
        "sAjaxSource":".../ExploreDatatable/loadTableAjax"  
    });

});
});

如何在JSON结构中删除这种不匹配?

感谢。

1 个答案:

答案 0 :(得分:0)

据我所知,你选择数据表是错误的。它应该这样做:

$('#emp').dataTable({
        "sAjaxSource":".../ExploreDatatable/loadTableAjax",
        "aoColumns": [{"mDataProp": "id"},
                      {"mDataProp": "firstName"},
                         .....the other column names, that should be mapped to the json key......
                     ]
    });