json object or associative array output
json object or associative array output
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
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
This discussion has been closed.
Replies
Allan
/* 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.
Running example: http://datatables.net/examples/data_sources/ajax.html
Allan
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?
Allan
{{"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.
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
{{"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
} );
}
} );
} );
{{"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"}}
[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