在jquery数据表中显示嵌套的JSON数据

在使用AJAX发出POST请求后,我得到以下JSON响应:

{ "ServiceName": "ABC", "Response": { "Object": [ { "Attributes": { "Attribute": [ { "AttributeName": "Name", "AttributeValue": "XYZ" }, { "AttributeName": "Place", "AttributeValue": "Abc" }, { "AttributeName": "Country", "AttributeValue": "Americas" }, { "AttributeName": "Code", "AttributeValue": "576" } ] } }, { "Attributes": { "Attribute": [ { "AttributeName": "Name", "AttributeValue": "XYZHJ" }, { "AttributeName": "Place", "AttributeValue": "Abchgh" }, { "AttributeName": "Country", "AttributeValue": "India" }, { "AttributeName": "Code", "AttributeValue": "536" } ] } } ] }} 

我使用datatable来显示数据..但是使用这个嵌套的JSON,我无法直接获取数据。 我使用这个https://datatables.net/examples/server_side/post.html https://datatables.net/reference/option/ajax.dataSrc作为参考。

您必须迭代响应并将其转换为dataTables可以理解的格式。 当我读取样本数据时,你有一个Object保存Attributes块,其中包含一个带有key => value对的AttributeName作为AttributeName => AttributeValue 。 所以在dataSrc回调中解析响应:

 var table = $("#example").DataTable({ ajax : { url : 'nestedData.json', dataSrc : function(json) { var temp, item, data = []; for (var i=0;i 

dataSrc回调返回表单上的对象数组:

 data = [ { Code: "576", Country: "Americas", Name: "XYZ", Place: "Abc" }, { Code: "536", Country: "India", Name: "XYZHJ", Place: "Abchgh" } ]