What is the simple way to show data from JSON arrays

What is the simple way to show data from JSON arrays

CosmoZCosmoZ Posts: 4Questions: 1Answers: 0
edited October 2015 in Free community support

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:

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    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.

  • allanallan Posts: 62,316Questions: 1Answers: 10,226 Site admin
    Answer ✓

    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

  • CosmoZCosmoZ Posts: 4Questions: 1Answers: 0

    @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?

  • CosmoZCosmoZ Posts: 4Questions: 1Answers: 0
    edited October 2015

    Hi,
    My problem got resolved by this way, it works well on my location, thanks for all your help.

    $.getJSON("./ajax/data/mydata.txt", function (data) {
        var dataTable = [];
        $.each(data, function(index, value) {
                dataTable.push(["", value[0]["system_model"], value[0]["kernel"]]);            
        });
            
    $(document).ready(function() {
        var table = $('#example').DataTable( {
            data: dataTable,
        ...
    
  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    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

  • CosmoZCosmoZ Posts: 4Questions: 1Answers: 0

    @jLinux
    Hi, I may consider to send the request as POST, will try to do that, thanks for your suggestion.

This discussion has been closed.