how to get data from json with tree format
how to get data from json with tree format
hi admin. i using example from datatables with json like:
{"id":"id_1",
"name":"name_1",
"job":"job_1",
"address":"address_1"
}
and its successfull and smart.
how about insert data from json with this format
{"id1":{
"name":"name_1",
"job":"job_1",
"address":"address_1"
},
"id2":{
"name":"name_2",
"job":"job_2",
"address":"address_2"
},
...
}
thanks.
example from script like this>>
<script>
$(document).ready(function() {
$('#currs').DataTable({
"ajax": {
"url": "https://www.zpool.ca/api/currencies",
"dataSrc": "",
},
"columns": [{
"data": "algo"
}, {
"data": "port"
}, {
"data": "name"
}, {
"data": "height"
}, {
"data": "workers"
}, {
"data": "shares"
}, {
"data": "hashrate"
}]
});
});
</script>
This question has accepted answers - jump to:
Answers
Hi @ycusoy ,
That's not possible, see the examples like this one - you can't have unique object names for each row. If there's no way to send the correct data, you could preparse and recreate the data on the client in the expected format.
Cheers,
Colin
@colin , is there no way that you can place the first row in one column then define the results of the data row in the first row, and continue on the second row onwards. like the repetition function to read json data.

Datatables expects the row data to be an array of objects or an array of arrays, as described here:
https://datatables.net/manual/data/#Data-source-types
Each array element is considered a row in Datatables. Even though it is a valid JSON string the JSON you have is not a supported data structure for Datatables:
You would need to convert the data structure to a supported structure. You can either do this in the server script or you could use
ajax.dataSrc
as a function to process the returned JSON into a supported structure. See the 3rd example in the docs.Kevin
@kthorngren, no idea and solution, I'm stuck in error.
First its a matter of learning how to iterate through your objects. Here is one of many tutorials:
https://zellwk.com/blog/looping-through-js-objects/
I chose to use the first example here:
http://live.datatables.net/kumixemu/1/edit
you could use something similar then return the generated array (
data
) instead ofjson.data
. There are probably better, more efficient ways to do this. Will let you decide if you need to do it differently based on performance.Kevin