JSON Single row, not picked up by datatables or ignored.

JSON Single row, not picked up by datatables or ignored.

Dabbo07Dabbo07 Posts: 3Questions: 0Answers: 0
edited May 2012 in DataTables 1.9
Hi, I'm having trouble understanding why dataTables is refusing to show a single row of data but it works perfectly fine for any data sets greater than 1 row. My debugger ID is 'ezunup' - I don't think this shows the issue either, as I think it's the format of the JSON message at fault here - The responses I captured were :

This is a 10 record JSON response which works OK:

[code]{"eventRetryLog":[{"eventstatus":"30","publisheventnumber":"102","retrycount":"0","retrymaximumcount":"10","retrytime":"14:54:19 20/02/12"},{"eventstatus":"30","publisheventnumber":"103","retrycount":"0","retrymaximumcount":"10","retrytime":"14:54:28 20/02/12"},{"eventstatus":"19","publisheventnumber":"125","retrycount":"10","retrymaximumcount":"10","retrytime":"15:22:53 20/02/12"},{"eventstatus":"19","publisheventnumber":"126","retrycount":"10","retrymaximumcount":"10","retrytime":"15:23:03 20/02/12"},{"eventstatus":"19","publisheventnumber":"127","retrycount":"10","retrymaximumcount":"10","retrytime":"15:24:19 20/02/12"},{"eventstatus":"19","publisheventnumber":"128","retrycount":"10","retrymaximumcount":"10","retrytime":"15:24:25 20/02/12"},{"eventstatus":"30","publisheventnumber":"148","retrycount":"0","retrymaximumcount":"10","retrytime":"17:26:13 20/02/12"},{"eventstatus":"19","publisheventnumber":"155","retrycount":"10","retrymaximumcount":"10","retrytime":"11:25:19 21/02/12"},{"eventstatus":"30","publisheventnumber":"165","retrycount":"0","retrymaximumcount":"10","retrytime":"11:41:46 21/02/12"},{"eventstatus":"19","publisheventnumber":"245","retrycount":"10","retrymaximumcount":"10","retrytime":"12:59:24 12/04/12"}],"iTotalDisplayRecords":"10","iTotalRecords":"10","sEcho":"1"} [/code]

This is a single row JSON response which display's "No matching records found":

[code] {"eventRetryLog":{"eventstatus":"30","publisheventnumber":"102","retrycount":"0","retrymaximumcount":"10","retrytime":"14:54:19 20/02/12"},"iTotalDisplayRecords":"1","iTotalRecords":"1","sEcho":"2"} [/code]

I've noticed there isn't a square bracket around the data on the single row data - so the elements merge into the tailing dataTable parameters. I'm not great with my JSON configuration so I'm kind of hoping to fix this on the dataTables side.

If it is a JSON formatting issue, any hints to help me fix it would be appreciated. I'm using Jersey v1.5 if this if relevant.

Many thanks in advance,

Darren.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    DataTables expects an array of items, event if the array has only one item in it (or zero for that matter). So the way to get it to work would be to have eventRetryLog always returned as an array.

    Allan
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It is possible to address this on the client-side, using fnServerData to reformat your response into the array that is needed - but I'd suggest changing the server code if you can.

    Allan
  • Dabbo07Dabbo07 Posts: 3Questions: 0Answers: 0
    Hi Allan,

    Thank you for the prompt response - I'll have a look into overriding the Jersey method(s) to correctly format the message - failing that, I'll look at your second suggestion :D

    I'll post a solution when I get to it just in case someone else encounters a similar problem.

    Thanks again and great plug-in btw ;)

    Darren.
  • Dabbo07Dabbo07 Posts: 3Questions: 0Answers: 0
    I found a solution that works for me, rather easily. Note that I'm new to Jersey and not sure how it all works (yet). Here is my solution to this problem and I hope it helps somebody.

    Apparently, my application's web.xml didn't specify a provider (which is OK) and it would default to using "JAXB JSON" and we need to use "Jackson" - I have no idea, but it worked by adding the following class path to my web.xml ...

    [code]

    com.sun.jersey.config.property.packages
    your.project.packages;org.codehaus.jackson.jaxrs

    [/code]

    Added : "org.codehaus.jackson.jaxrs" to the init-param section.

    For a better explanation, please refer to the original source:

    Reference : http://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json/3143214#3143214

    Thanks, Darren.
This discussion has been closed.