Richard Richard - 1 year ago 56
jQuery Question

parse json array which contains sql array

My question: How can I parse a json array?

I send it like this:

$data= mysql_fetch_array($r);
header('Content-type: application/json');

"check": "<?php echo $check?>",
"productcode": "<?php echo $code?>",
"values": [<?php echo $data?>]

on the ajax side I do this on the single values which works fine

success: function(data,textStatus){
//return values

if I want to access the array I do something like this and I get undefined


What am I doing wrong?


You need to encode your array as JSON, too:

    "check": "<?php echo $check?>",
    "productcode": "<?php echo $code?>",
     "values": [<?php echo json_encode($data); ?>]

The easiest way is probably to put everything in a PHP array and serialize this as JSON:

$output = array(
    "check" => $check,
    "productcode" => $code,
    "values" => $data

echo json_encode($output);

Then you can be sure that it is being encoded properly.