无法检测到#34;意外标识符"在parseJSON中

时间:2015-03-17 07:36:25

标签: php jquery json parsing

我正在尝试从数据库中获取数据并执行json_encode

    $data = json_encode($results);

当我输入echo($ data);死();我得到了以下结果

    [{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Wa","PRODUCT_GROUP":"ACP'S","NET_SALES":"187002.04","RANK":"1"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"CVS","PRODUCT_GROUP":"ACP'S","NET_SALES":"127948.68","RANK":"2"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"McK","PRODUCT_GROUP":"ACP'S","NET_SALES":"81079.29","RANK":"3"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Car","PRODUCT_GROUP":"ACP'S","NET_SALES":"65320.42","RANK":"4"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Krog.","PRODUCT_GROUP":"ACP'S","NET_SALES":"31977.95","RANK":"5"}]

在jquery代码中我试图解析JSON $数据,如下所示

    $(function () {
     var data = new Array();
     data = $.parseJSON('<?php echo $data; ?>'); //error occuring here
    //other code goes here
    });

我在Uncaught SyntaxError: Unexpected identifier

附近收到错误data = $.parseJSON('<?php echo $data; ?>');

我在

中获得以下输出
  [{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Wa","PRODUCT_GROUP":"ACP'S","NET_SALES":"187002.04","RANK":"1"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"CVS,Inc.","PRODUCT_GROUP'S":"ACP","NET_SALES":"127948.68","RANK":"2"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"McK","PRODUCT_GROUP":"ACP'S","NET_SALES":"81079.29","RANK":"3"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Car","PRODUCT_GROUP":"ACP'S","NET_SALES":"65320.42","RANK":"4"},{"CAL_DATE":"01-JUN-13","CUSTOMER_TEXT":"Krog.","PRODUCT_GROUP":"ACP'S","NET_SALES":"31977.95","RANK":"5"}]

任何人都可以告诉我为什么我会收到该行的错误?提前致谢

1 个答案:

答案 0 :(得分:1)

这是一个有效的json,所以你不需要解析它:

data = <?php echo $data; ?>

我想你想这样用它:

$(function () {
 var data = '<?php echo $data; ?>';
 data = $.parseJSON(data); //error occuring here
//other code goes here
});

来自文档:

  

采用格式正确的JSON字符串 返回生成的 JavaScript值

从文档中查看:

var obj = jQuery.parseJSON( '{ "name": "John" }' ); // results in {"name" : "John"}
alert( obj.name === "John" );

'{ "name": "John" }'是一个json格式的字符串。