What is the simple way to show data from JSON arrays
What is the simple way to show data from JSON arrays
CosmoZ
Posts: 4Questions: 1Answers: 0
The JSON arrays likes:
{
"111":[
{
"kernel": "Ol6",
"system_model": "n/a",
"driver": "n/a",
"guest_os": "n/a",
"system_meter": {},
"device": "n/a",
"sub_array": [
{
"name": "start",
"output": ""
},
{
"name": "stop",
"output": ""
}
]
}
],
"222":[
{
"kernel": "OL5.x86_64",
"system_model": "n/a",
"driver": "n/a",
"guest_os": "n/a",
"system_meter": {},
"device": "n/a",
"sub_array": [
{
"name": "kill",
"output": ""
},
{
"name": "restart",
"output": ""
}
]
}
]
}
My testing code:
var table = $('#example').DataTable( {
"ajax": {
url: "./ajax/data/data.txt",
dataSrc: ""
},
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "system_model" },
{ "data": "kernel" }
],
"order": [[1, 'asc']]
} );
Regarding function "ajax.dataSrc", looks like it only can show individual array, such as: dataSrc: "111" or dataSrc: "222".
How to show all of the arrays data once?
This question has accepted answers - jump to:
This discussion has been closed.
Answers
It would be a bit easier to read if you marked that JSON as plain text.
Take a look at the examples on nested arrays/objects.
https://datatables.net/examples/ajax/objects_subarrays.html
Since you don't have a top level object, I think you can skip the dtrSrc. You will need some dotted object notation and probably some array notation - [0] - to get deep into the object.
I use the debugger in Chrome to put in a break point and then I just play with the syntax in the "watch" window until I get what I need.
I've changed the format of the post now. This tech note explains how markdown can be used to format posts.
DataTables currently does not accept objects as the list of data for the rows. A row's data can be an object, but the list of rows must be an array. You would need to convert from the objects to an array to use that data structure - the
ajax.dataSrc
option can be used for that.Allan
@allan, @ThomD
Hi,
Thanks for your replied, so I need to convert my testing data from the json objects {[], [], ... } to an javascript array[[], [], ...], then using the javascript array as the Datatables sourced data, right?
Hi,
My problem got resolved by this way, it works well on my location, thanks for all your help.
Nice, I think you might also be able to do something fancy like parse the data inside a closure as a value for the
ajax
@jLinux
Hi, I may consider to send the request as POST, will try to do that, thanks for your suggestion.