json object or associative array output

json object or associative array output

alcomalcom Posts: 9Questions: 0Answers: 0
edited March 2011 in General
Hi,

Is there any way for datatables (PHP) to process json output containing objects or associative arrays?

{"Input":{"type1":"0","type2":"StaticTour","type3":false},"Send":{"text":null,"type":null,"source":null},"Get":{"p":{"type1":"0","type2":"StaticType","type3":false}},"Result":true}

Thanks

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    No way that is built in to DataTables at the moment - you would need to use fnServerData ( http://datatables.net/usage/callbacks#fnServerData ) to transform the data into the 2D array structure needed by DataTables.

    Allan
  • alcomalcom Posts: 9Questions: 0Answers: 0
    Can you show an example of json output that could work? Will the above posted output work?

    /* Add some data to send to the source, and send as 'POST' */
    aoData.push( { "name": "my_field", "value": "my_value" } );

    Should I change anything in the aoData.push code?

    Thank you.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Data source: http://datatables.net/examples/examples_support/json_source.txt
    Running example: http://datatables.net/examples/data_sources/ajax.html

    Allan
  • alcomalcom Posts: 9Questions: 0Answers: 0
    I am still confused:

    Will fnServerData transform

    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}

    into:

    { [["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"]]}

    Or will it process the first piece of code as is?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    No - you need to do the transform inside fnServerData. There is an example of how to use fnServerData for this kind of thing here: http://datatables.net/plug-ins/server-data-formats .

    Allan
  • alcomalcom Posts: 9Questions: 0Answers: 0
    The example does not show how to transform the data from

    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}

    into:

    { [["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"]]}


    Man, I am totally lost. Very cryptic.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    It doesn't show explicitly how to do the transformation, just the general concept of how it can be done. You might be the first to do this exact transformation, so it will require some custom code :-)

    So first of all, I presume that the code:

    [code]
    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}
    [/code]
    Should be:

    [code]
    [{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}
    [/code]
    Since what you had before isn't valid JSON. At that point you just need to loop over the array and create the 2D array.

    [code]
    aOut.push( aIn[i]["0"], aIn[i]["1"], aIn[i]["2"], ... );
    [/code]
    For example.

    Allan
  • alcomalcom Posts: 9Questions: 0Answers: 0
    This is what I have in my json_source.text file:

    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}

    Then I added this to ajax.html in the examples folder and it did not work:

    $(document).ready(function() {
    $('#example').dataTable( {
    "sAjaxSource": "../examples_support/json_source.txt",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some data to send to the source, and send as 'POST' */
    aoData.push( aIn[i]["0"], aIn[i]["1"], aIn[i]["2"], aIn[i]["3"], aIn[i]["4"] );
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    } );
    } );
  • alcomalcom Posts: 9Questions: 0Answers: 0
    By the way

    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}

    should be

    { [["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"]]}

    or whatever format will work with datatables.

    Below is just the output from the target server but I my goal is to get this data processed by datatables if it is at all possible as I cannot modify how the server outputs json.

    {{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}
  • alcomalcom Posts: 9Questions: 0Answers: 0
    I guess I need to insert square brakets and get rid of indexes.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    It might be worth running your JSON through the JSON validator: http://jsonlint.com/ - none of the formats in your previous posts are valid :-). I believe you are looking for the format I specified earlier:

    [code]
    [{"0":"Trident","1":"Internet Explorer 4.0","2":"Win 95+","3":"4","4":"X"},
    {"0":"Trident","1":"Internet Explorer 5.0","2":"Win 95+","3":"5","4":"C"}}
    [/code]
    DataTables needs this format:

    [code]
    [["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"]]
    [/code]
    With the line I gave you before, you need to loop over your array (whenever it might be called, I gave the place holder name of aIn), and you need to replace success:fnCallback with your function which will consider from the first format above, to the second (you've put the line I gave you just after the comment "/* Add some data to send to the source, and send as 'POST' */" - you aren't sending it to the server, you are trying to transform what comes back from the server.

    Allan
This discussion has been closed.