How do I get a JSON object named aaData

How do I get a JSON object named aaData

mcubedmcubed Posts: 5Questions: 0Answers: 0
edited January 2011 in General
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]

Replies

  • allanallan Posts: 63,192Questions: 1Answers: 10,412 Site admin
    Have a look at the JSON returned by this example (using Firebug or similar): http://datatables.net/examples/data_sources/ajax.html .

    Allan
  • netmannetman Posts: 15Questions: 0Answers: 0
    edited January 2011
    Try this way:

    [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
  • mcubedmcubed Posts: 5Questions: 0Answers: 0
    THank you for the responses all but none of them really answered my question.

    @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,
  • mcubedmcubed Posts: 5Questions: 0Answers: 0
    OK so here is a bit more...

    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
  • netmannetman Posts: 15Questions: 0Answers: 0
    edited January 2011
    OK ...

    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
  • mcubedmcubed Posts: 5Questions: 0Answers: 0
    Thanx for getting back to me but unfortuently asking my provider for a second XML is not an option... The output was decided on by people higher up than me. All I have is this output. I have to come up with a way to work with it as is.
  • netmannetman Posts: 15Questions: 0Answers: 0
    edited January 2011
    Nice !!!
    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
  • mcubedmcubed Posts: 5Questions: 0Answers: 0
    OK So I went back in to try and echo the xml that is being returned and it turns out there is non it is a straight json string.

    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.
This discussion has been closed.