使用没有数据头对象的ajax解析嵌套json的数据表



我正在尝试解析我在Python中创建的JSON对象,并使用ajax将其填充到Datatables中。它没有将数据解析到列中,但我怀疑这可能是因为我的JSON结构,因为它没有";数据";标头对象?我是否正在访问ajax中的数据更正?

这是我的AJAX部分

$(document).ready(function() {
$.fn.dataTable.ext.errMode = 'none';
var table = $('#ce_table').DataTable( {
"processing": true,
"ajax": "/api/1.0/agents",
"columns": [
{ "data": "machine.hostname" },
{ "data": "machine.epm" }
]
} );
});

这就是我的数据结构的样子

[
{
"machine": {
"hostame": "machine0101",
"epm": "0"
}
},
{
"machine": {
"hostame": "machine0202",
"epm": "1"
}
}
]

问题实际上是JSON的结构,当你没有像"数据";在JSON结构中。

新JSON示例:

{
"data": [
{
"machine": {
"hostname": "machine111"
},
"content": {
"epm": "0"
}
},
{
"machine": {
"hostname": "machine222"
},
"content": {
"epm": "1"
}
}
]
}

这是通过更改构建JSON的python代码来实现的,请参阅以下内容:

def agents_api():
_tmp = []
_dict = {}
_final_dict = {}
_nested_machine = {}
_nested_content = {}
epm_result = #A list ('machine111', machine999')
machine_result = #A list ('machine111', machine222')
for machine in machine_result:
if machine in epm_result:
_nested_machine["hostname"] = machine
_nested_content['epm'] = "1"
else:
_nested_machine["hostname"] = machine
_nested_content['epm'] = "0"
_dict['machine'] = _nested_machine
_dict['content'] = _nested_content
_tmp.append(_dict) <--- put it together in a list, this is the 'nested' part
_final_dict['data'] = _tmp <--- create the parent "data" object
json_data = dumps(_final_dict) <--- convert to JSON object
print(json_data)
return json_data <--- send back

最新更新