How do I get a JSON object named aaData
How do I get a JSON object named aaData
First off let me say I am a newbie when it comes to JQuery and to JSON. I am trying to display the results of a custom built web service call in a table. I don't want to display all of the columns only a subset of them in the end but right now I would be happy displaying them all.
Anyway I followed the documentation as best I could and I am still lost. The AJAX example says " DataTables expects an object with an array called "aaData" with the data source." How do I do that? The web service I hit returns the JSON string (Below) but it is not called aaData. How do I force it to be or force DataTables to use my string instead of looking for one called aaData?
The error I am getting (in both FF and IE) is:
Message: 'aaData.length' is null or not an object
Here is the code I am using to initilize the table:
[code]$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": '%%GLOBAL_ShopPath%%/templates/default/cineupload.php'
} );
} );[/code]
Here is a subset of the json:
[code]{"ReturnCode":1,"Results":"[{\"ContentID\":1,\"CustomerID\":1,\"Source\":\"C:\\\\TEMP\\\\Sample.mov\",\"Status\":\"SUBMITTED\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"Updated on... 1/4/2011 7:13:42 PM\"},{\"ContentID\":2,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":3,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":4,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":5,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":6,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":7,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":8,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},...[/code]
Anyway I followed the documentation as best I could and I am still lost. The AJAX example says " DataTables expects an object with an array called "aaData" with the data source." How do I do that? The web service I hit returns the JSON string (Below) but it is not called aaData. How do I force it to be or force DataTables to use my string instead of looking for one called aaData?
The error I am getting (in both FF and IE) is:
Message: 'aaData.length' is null or not an object
Here is the code I am using to initilize the table:
[code]$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": '%%GLOBAL_ShopPath%%/templates/default/cineupload.php'
} );
} );[/code]
Here is a subset of the json:
[code]{"ReturnCode":1,"Results":"[{\"ContentID\":1,\"CustomerID\":1,\"Source\":\"C:\\\\TEMP\\\\Sample.mov\",\"Status\":\"SUBMITTED\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"Updated on... 1/4/2011 7:13:42 PM\"},{\"ContentID\":2,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":3,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":4,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":5,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":6,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":7,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":8,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},...[/code]
This discussion has been closed.
Replies
Allan
[code]
$json_array = array("aaData" => array(
array( "$cell_1","$cell_2", "$cell_3" , "$cell_4", etc ...),
array( "$cell_1","$cell_2", "$cell_3" , "$cell_4", etc ...),
etc ...
)
);
json_encode($json_array);
[/code]
Just put the values in your array/table, you don't need "ContentID\",\"CustomerID\",\"Source\",\"Status\":\"SUBMITTED\",\"FrameRate" ... in the data table. All these may be your table's Header i.e:
[code]
"aoColumns": [
/* ContentID */ { "bSearchable": true, "bVisible": true },
/*CustomerID*/ { "bSortable": true, "bSearchable": true, "bVisible": true },
/* Source */ { "bSortable": true, "bSearchable": true, "bVisible": true },
/* Status */ { "bSortable": true, "bSearchable": true, "bVisible": true },
etc ...
[/code]
Regards
Steve
@Allan.... I have seen that soap already not sure what you expect me to see
@Netman... You are the closest but as I stated the JSON is being given to me by a a custom web service. I am not writting it so I have no control over what goes into what cell. (though you did give me an idea to try).
@Steve... I don't understand what you are saying at all.
THanx again all and if I sound grouchy it is because this was supossed to be done two days ago and I have not had coffee yet this morning,
Netman..
I played a little in PHP and modified my code to do this"
[code]
$response = stream_get_contents($fp);
$xml = simplexml_load_string($response);
$json_array = array("aaData" => array(json_decode($xml)));
echo json_encode($json_array);
[/code]
here is the new json I am echoing out:
[code]
{"aaData":[{"ReturnCode":1,"Results":"[{\"CustomerID\":1,\"Source\":\"C:\\\\TEMP\\\\Sample.mov\",\"ContentID\":1,\"Status\":\"SUBMITTED\",\"FrameRate\":0,\"Frames\":0,\"Sec\":0,\"FrameDelay\":0,\"Lossy\":0,\"Color\":0,\"Width\":0,\"Height\":0,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"Updated on... 1\/4\/2011 7:13:42 PM
[/code]
but I still get the same error
You need to ask your "xml provider" (You wrote: ...JSON is being given to me by a a custom web service) to
give you a second xml, let's create for this a new variable: $response_clean with a new "file" named $fp_clean
This one (the second xml file) should contain ONLY the "data" without "discriptions".
Example:
Let's take the first element: \"CustomerID\":1
This is a "discription" (or Helper ... call it whatever you like): \"CustomerID\":
This is the coresponding "data" part: 1
Plus ... you don't need the: "ReturnCode":1,"Results"
Instead of these you may ask your "provider" to send you (in the $fp_clean) only: "aaData".
So, after all the above you may export the json "data" like that:
[code]
$response_clean = stream_get_contents($fp_clean);
$xml_clean = simplexml_load_string($response_clean);
$json_array = array(array(json_decode($xml_clean)));
echo json_encode($json_array);
[/code]
Hope that helps.
Steve
All you have to do ... is to "clean" the xml file by yourself.
But this is strictly "php" not a DataTables issue ;-).
Parse the xml file to a "new variable" (It will be an array) and then clean up the "unwanted" elements (The keys of the array maybe?) and finaly
use this "new array - variable" as the input to json_encode.
Ciao
Steve
Here is my modified PHP to print it out
[code]
$response = stream_get_contents($fp);
//$xml = simplexml_load_string($response);
echo $response;
[/code]
and here is what I get:
[code]
{"ReturnCode":1,"Results":"[{\"ContentID\":601,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":175,\"Sec\":14,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":602,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\cooking2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":97,\"Sec\":8,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":603,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":175,\"Sec\":14,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":604,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\cooking2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":97,\"Sec\":8,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":605,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":175,\"Sec\":14,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"},{\"ContentID\":606,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\cooking2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":97,\"Sec\":8,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"}]","Message":"Success"}
[/code]
I still do not understand how I take each element (":"[{\"ContentID\":601,\"CustomerID\":1,\"Source\":\"c:\\\\uploader\\\\Leeza_M-cubed Designs\\\\Bike2.mov\",\"Status\":\"NEW\",\"FrameRate\":12,\"Frames\":175,\"Sec\":14,\"FrameDelay\":1,\"Lossy\":10,\"Color\":256,\"Width\":512,\"Height\":384,\"ExtendedCompression\":0,\"Compression\":0,\"Results\":\"\"}) and parse it into an array... as I said I am kinda new to all of this.. maybe I am just over complicating it but I cannot seem to get it.