weixin_33691598 2016-09-14 13:44 采纳率: 0%
浏览 14

JSON对数据表的响应?

I have the following example to get XML response from Ajax call and appending to table.

$("#ajaxform5").submit(function(e) {
    var formURL = $(this).attr("action");
    var request = $.ajax({
        url : formURL,
        type: "GET",
        contentType : "application/xml",
        headers: {
            "Type" : "events"
        },
        success: function(xml){
            $(xml).find('event').each(function(){
                var $event = $(this);
                var id = $event.find("id").text();
                var level = $event.find("level").text();
                var name = $event.find("name").text();
                var attributes01 = $event.find("attributes").children("entry").eq(0).children("string").eq(0).text();
                var attributes02 = $event.find("attributes").children("entry").eq(0).children("string").eq(1).text();
                var attributes11 = $event.find("attributes").children("entry").eq(1).children("string").eq(0).text();
                var attributes12 = $event.find("attributes").children("entry").eq(1).children("string").eq(1).text();
                var attributes21 = $event.find("attributes").children("entry").eq(2).children("string").eq(0).text();
                var attributes22 = $event.find("attributes").children("entry").eq(2).children("string").eq(1).text();
                var attributes31 = $event.find("attributes").children("entry").eq(3).children("string").eq(0).text();
                var attributes32 = $event.find("attributes").children("entry").eq(3).children("string").eq(1).text();
                var attributes41 = $event.find("attributes").children("entry").eq(4).children("string").eq(0).text();
                var attributes42 = $event.find("attributes").children("entry").eq(4).children("string").eq(1).text();
                var userId = $event.find("userId").text();
                var ipaddress = $event.find("ipaddress").text();
                var outcome = $event.find("outcome").text();
                var html = '<tr><td>' + id + '</td><td>' + level + '</td><td>' + name + '</td><td>' +
                    '<div><span class="title">' + attributes01 + '</span> <span>' + attributes02 + '</span></div>' +
                    '<div><span class="title">' + attributes11 + '</span> <span>' + attributes12 + '</span></div>' +
                    '<div><span class="title">' + attributes21 + '</span> <span>' + attributes22 + '</span></div>' +
                    '<div><span class="title">' + attributes31 + '</span> <span>' + attributes32 + '</span></div>' +
                    '<div><span class="title">' + attributes41 + '</span> <span>' + attributes42 + '</span></div>' +
                    '</td><td>' + userId + '</td><td>' + ipaddress + '</td><td>' + outcome + '</td></tr>';
                $('#eventTable').append(html);
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
});

Now I want to use Datatables. So I convert response XML to JSON with xml2json(abdmob/x2js) and I got the JSON response below.

JSON response;

{
    "list" : {
        "event" : [{
                "dateTime" : "1473858435162",
                "nanoTime" : "438326047248251",
                "id" : "15680",
                "eventTime" : {
                    "time" : "1473851193487",
                    "timezone" : "Asia/Istanbul"
                },
                "level" : "INFORMATION",
                "name" : "Get message content",
                "attributes" : {
                    "entry" : [{
                            "string" : ["metaDataIds", "6"]
                        }, {
                            "string" : ["messageId", "2765"]
                        }, {
                            "string" : ["channel", "Channel[id=44ab0667-d42c-496c-bca2-2bd7eecd6dc9,name=API jQuery Direct]"]
                        }
                    ],
                    "_class" : "linked-hash-map"
                },
                "outcome" : "SUCCESS",
                "userId" : "1",
                "ipAddress" : "127.0.0.1",
                "serverId" : "f85e48e2-32b3-429f-bee5-31630d8143a2"
            }, {
                "dateTime" : "1473858435167",
                "nanoTime" : "438326052034149",
                "id" : "15679",
                "eventTime" : {
                    "time" : "1473851191973",
                    "timezone" : "Asia/Istanbul"
                },
                "level" : "INFORMATION",
                "name" : "Get messages by page limit",
                "attributes" : {
                    "entry" : [{
                            "string" : ["filter", "MessageFilter[maxMessageId=2765,minMessageId=<null>,originalIdUpper=<null>,originalIdLower=<null>,importIdUpper=<null>,importIdLower=<null>,startDate=<null>,endDate=<null>,textSearch=<null>,textSearchRegex=<null>,statuses=<null>,includedMetaDataIds=[6],excludedMetaDataIds=<null>,serverId=<null>,contentSearch=[],metaDataSearch=<null>,textSearchMetaDataColumns=<null>,sendAttemptsLower=<null>,sendAttemptsUpper=<null>,attachment=false,error=false]"]
                        }, {
                            "string" : ["includeContent", "false"]
                        }, {
                            "string" : ["offset", "0"]
                        }, {
                            "string" : ["limit", "21"]
                        }, {
                            "string" : ["channel", "Channel[id=44ab0667-d42c-496c-bca2-2bd7eecd6dc9,name=API jQuery Direct]"]
                        }
                    ],
                    "_class" : "linked-hash-map"
                },
                "outcome" : "SUCCESS",
                "userId" : "1",
                "ipAddress" : "127.0.0.1",
                "serverId" : "f85e48e2-32b3-429f-bee5-31630d8143a2"
            }
        ]
    }
}


So my first question, is this JSON response valid for DataTables?
If it is valid, how can I address objects and arrays to row / columns like in my first example?

I tried to insert JSON response to Datatables but I couldn't. You can see the result in the picture. response added to rows char by char

I gave the code below for illustration only, you do not want to know how much I drooled to insert data.
I also tried columns.data option to read arrays but failed again.

$("#ajaxform6").submit(function(e) {
    var formURL = $(this).attr("action");
    var request = $.ajax({
        url : formURL,
        type: "GET",
        contentType : "application/xml",
        headers: {
            "Type" : "events"
        },
        success: function(data, textStatus, jqXHR) {
            var x2js = new X2JS();
            var bXML = jqXHR.responseText;
            var jsonObj = x2js.xml_str2json(bXML);
            var json = JSON.stringify(jsonObj);
            console.log(json);
            $('#table_id').DataTable( {
                data: json,
                bSort: false,
            } );
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
});
  • 写回答

2条回答 默认 最新

  • weixin_33691817 2016-09-14 13:51
    关注
    • You don't need to use JSON.stringify
    • You need to specify data property for each column with columns.data property

    Sample code for 2-column table is shown below. Add more column.data entries for additional columns.

    $('#table_id').DataTable( {
       data: jsonObj.list.event,
       columns: [
          { data: 'name' },
          { data: 'level' }
       ]
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢