Json Format

Json Format

Alex_hAlex_h Posts: 3Questions: 0Answers: 0
edited April 2014 in General
Hi,

I have a json file lokks like :
[code]
[
{
"name":"Serveur",
"nom":
[
"name1",
"name2"
]
},
{
"name":"Horaire dépassé",
"nombre":
[
209,
197,
88,
57,
30,
24,
21,
17,
17,
15
]
}
]
[/code]

I can't modify this file. It was used for others stuff.

I unable to create datatable with this file .
I do several test without result :

[code]
$.getJSON('include/stats_top10_hd_month.json',function(data){
console.log(data) ;
var r = data ;
console.log(r[0]["nom"])
$("#tab_top10_hd_month").dataTable({
"aaData" : r,
"bProcessing": true,
"aoColums": [
{"mData": "nom"},
{"mData": "nombre"}
]
})
}) ;
[/code]

Does someone can help me ?

PS: Sorry for my english .

Replies

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    In the array given to DataTables, each entry is an array. So in your array above, there would be two rows, with different objects. Is that want you want?

    Allan
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Ah no - I've just realised. You would need to use Javascript to convert from your array format to an array of objects that look like:

    [code]
    {
    "nom": ...,
    "nombre": ...
    }
    [/code]

    Allan
  • Alex_hAlex_h Posts: 3Questions: 0Answers: 0
    Hi Allan,

    That is . I would a table with columns "Nom" and "Nombre" .

    But I don't know how i have to do.
    I think i need convert but i don't know what exactly and how .

    thx
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    edited March 2014
    Just loop over the objects:

    [code]
    var a = [];

    for ( var i=0, ien=d[0].nom.length ; i
  • Alex_hAlex_h Posts: 3Questions: 0Answers: 0
    Thanks a lot .
    It's good for me .

    This format is used for draw several graphs with highcharts.
This discussion has been closed.