如何为 jQuery 数据表获取 JSON 的某些部分

How to get some part of JSON for jQuery DataTables

我在 jQuery DataTables aaData 属性 中使用 webapi,如下所示:

 "aaData": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",

这个请求return一个JSON像这样:

{
"return":
    {"status":200,"message":"Accept"},
"entries":
[
    {"messageid":40859622,"message":"Text1","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146},
    {"messageid":40859621,"message":"Text2","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146},
    {"messageid":40859589,"message":"Text3","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868620,"cost":146},
    {"messageid":40858860,"message":"Text4","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867960,"cost":146},
    {"messageid":40858483,"message":"Text5","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867600,"cost":146}
]
}

但我只需要 "entries" 的行将其传递给我的 aaData,如下所示:

    {"messageid":40859622,"message":"Text1","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146},
    {"messageid":40859621,"message":"Text2","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868680,"cost":146},
    {"messageid":40859589,"message":"Text3","status":14,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431868620,"cost":146},
    {"messageid":40858860,"message":"Text4","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867960,"cost":146},
    {"messageid":40858483,"message":"Text5","status":10,"statustext":"Waiting","sender":"300007575","receptor":"0911111111","date":1431867600,"cost":146}

我如何工作

"aaData": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",

获取我需要的值?

function AssignResult() {

    //Call Web API
    var result = { //Get the Result
        "return":
        { "status": 200, "message": "Accept" },
        "entries":
        [
            { "messageid": 40859622, "message": "Text1", "status": 14, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868680, "cost": 146 },
            { "messageid": 40859621, "message": "Text2", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868680, "cost": 146 },
            { "messageid": 40859589, "message": "Text3", "status": 14, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431868620, "cost": 146 },
            { "messageid": 40858860, "message": "Text4", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431867960, "cost": 146 },
            { "messageid": 40858483, "message": "Text5", "status": 10, "statustext": "Waiting", "sender": "300007575", "receptor": "0911111111", "date": 1431867600, "cost": 146 }
        ]
    };

    var aaData = result.entries; //aaData will have all the entries.

}

希望对您有所帮助。

您需要使用 sAjaxDataProp (DataTables 1.9) or ajax.dataSrc (DataTables 1.10),以下是手册的摘录:

By default DataTables will look for the property 'aaData' when obtaining data from an Ajax source or for server-side processing - this parameter allows that property to be changed. You can use Javascript dotted object notation to get a data source for multiple levels of nesting.

下面是针对不同 DataTables 版本的正确初始化代码。

数据表 1.9.x

$(document).ready(function() {
  var oTable = $('#example').dataTable({
    "sAjaxSource": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",
    "sAjaxDataProp": "entries"
  });
});

数据表 1.10.x

$(document).ready(function() {
  var oTable = $('#example').dataTable({
    "ajax": {
        "url": "http://api.example.com/v1/asdasd/sms/latestoutbox.json?pagesize=1000",
        "dataSrc": "entries"
    }
  });
});