record fields containing " (double quotes) in json source

record fields containing " (double quotes) in json source

helmerjhelmerj Posts: 3Questions: 0Answers: 0
edited November 2010 in General
Hi,

I am using datatables (1.7.4) in an rails3 application. I have large datasets and there for use an sAjaxSource for feeding the data in JSON format to the dataTable function. ON a particular dataset (customer created) I got tons of parsing errors:
[code]
DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
[/code]
It turns out that those data-sets to be parsed, contained double quotes (") in one of the fields to be displayed thereby screwing up the json format. Is there a quick way to escape those (central in my datatable code) or do I have to replace them on record creation in all of my rails controllers?

Thanks in advance for any help on this

Cheers J.

Replies

  • gresekorgresekor Posts: 2Questions: 0Answers: 0
    Hi,

    when generating json in PHP, why don't you use 'json_encode($response)' function:
    http://php.net/manual/en/function.json-encode.php

    [code]
    <?php

    ...

    $result = $db->query($query);

    $aaData = array();
    if ($result->num_rows != 0){
    while ($record = $result->fetch_array(MYSQLI_NUM)) {
    $aaData[] = $record;
    }
    }

    // response in json to be returned
    $response = array(
    "query" => $query,
    "sEcho" => intval(mysqli_real_escape_string($db, $_GET['sEcho'])),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => $aaData
    );

    //header('Content-type: application/json');
    header('Content-type: text/html');
    echo json_encode($response);
    ?>
    [/code]

    Best, Gregor
  • helmerjhelmerj Posts: 3Questions: 0Answers: 0
    Hi Gregor,

    well, because I am no using PHP but Ruby on Rails :-)
    But I will investigate if there is something similar in Rails.
    Cheers Juergen
This discussion has been closed.