将json数组转换为数据格式

时间:2011-02-24 14:10:58

标签: javascript jquery json datatables

我的currrent数组格式没有被datatables aaData格式解释为im传递列值:

{
    "aaData": [
        {
            "startDate": "09/08/2010 12:00:00 AM",
            "endDate": "13/08/2010 12:00:00 AM",
            "runDate": "16/08/2010 12:00:00 AM",
            "clientId": "40272",
            "clientType": "C",
            "plannerName": "Adrian Mcfly",
            "plannerRegion": "s1",
            "contact": "Vera chaniqua",
            "email": " ",
            "interviewDate": "09/08/2010 12:00:00 AM"
        },
    ]
}

如何删除列ID并仅显示值,以便数据表可以将其作为ajax调用读取?

3 个答案:

答案 0 :(得分:7)

编辑2012年8月29日

从1.9开始,您可以禁用必须拥有根JSON属性。

"sAjaxDataProp": "",

这可能是大多数JSON序列化程序的结果。

或自定义

"sAjaxDataProp": "myData",

在datatables 1.8中,你可以像这样格式化你的json:

{
        "aaData": [
            {
                "DT_RowClass": "",
                "description": "",             
                "pkgLineTree": {
                    "treeId": {
                        "name": "Jacksonville"
                    }
                }              
            },
            {
                "DT_RowClass": "",
                "description": "",       
                "pkgLineTree": {
                    "treeId": {
                        "name": "Jacksonville"
                    }
                }         
            }

        ]
    }

在您的数据表属性中添加此

"aoColumns": [    
        {
            "mDataProp": "pkgLineTree.treeId.name"
        },  
        {
            "mDataProp": "shortname"
        },
        {
            "mDataProp": "description"
        }, 
        {
            "mDataProp": "lineStatus"
        }
        ],    

答案 1 :(得分:1)

嗯,基本上你所拥有的是一个对象数组的JSON表示(具有属性 startDate endDate ,. ..),但你需要的是一个数组字符串数组。

我假设您正在进行服务器端处理,因此如果您不想更改服务器代码,您可以直接在获取过程的中间并在将数据提供给回调之前修改数据数据表。

接下来我要做的就是浏览获取数据中的每个对象并创建值数组:

$('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
        $.getJSON( sSource, aoData, function (json) { 
            /* --- Here is where we massage the data --- */
            /* if the variable "json" is just a string (I forgot) then evaluate it first into an object (download a json library)*/
            var aaData=[];
            $.each(json, function(index, object) {
                var aData=[];
                $.each(object, function(key, value) { 
                    aData.push(value); //be careful here, you might put things in the wrong column 
                });
                aaData.push(aData);
            }); 
            /* --- And after we're done, we give the correctly formatted data to datatables --- */  /* --- if "json" was a string and not an object, then stringify aaData first (object to json notation, download a library) --- */
            fnCallback(aaData)
        } );
    }
} );

});

希望它有效!

答案 2 :(得分:0)

尝试使用方括号而不是打开/关闭花括号{和}。有关详细信息,请参阅我对此问题的回答:datatables and json formatting error with php

相关问题