[英]Parsing JSON : unexpected character
This question is related to a previous one I wrote here . 这个问题与我在这里写的上一个问题有关。
Is this JSON syntax correct ? 这个JSON语法正确吗? I need it to make a jqPlot chart after. 之后,我需要它来制作jqPlot图表 。
[{"Date":"2012-02-29","Close":"87.60"},{"Date":"2012-02-28","Close":"87.77"},{"Date":"2012-02-27","Close":"88.07"}]
I ask this because I can't use jQuery.parseJSON(jsonString);
我问这个是因为我不能使用jQuery.parseJSON(jsonString);
or JSON.parse(jsonString);
或JSON.parse(jsonString);
with this string. 用这个字符串。 Firefox returns : Firefox返回:
SyntaxError: JSON.parse: unexpected character @ index2.php:677 语法错误:JSON.parse:意外字符@ index2.php:677
Here is the PHP code that generates it : 这是生成它的PHP代码:
<?php
$req = $bdd->prepare('SELECT Date, Close FROM quotes WHERE Symbol = ? AND Date > ? AND Date < ?');
$req->execute(array($_GET['id'], $_GET['datemin'], $_GET['datemax']));
$test=array();
while ($donnees = $req->fetch(PDO::FETCH_ASSOC))
{
// echo print_r($donnees) . "<br />";
// echo $donnees[Date] . "<br />";
$test[] = $donnees;
}
echo json_encode($test);
?>
I don't know what's wrong. 我不知道怎么了
EDIT : Javascript code added. 编辑:添加了Javascript代码。
<script>
$("button").click(function(){
$.get("requete_graph.php", {
id: param1,
datemin: param2,
datemax: param3
}, function(data,status){
console.log(data);
make_graph(data);
}, "json");
});
function make_graph(toto) {
alert("String before : " + JSON.stringify(toto));
var json_parsed = JSON.parse(toto);
alert("String after : " + JSON.stringify(json_parsed));
$(document).ready(function(){
var plot1 = $.jqplot('chartdiv', json_parsed);
});
}
</script>
The JSON is indeed valid (you can check it at jsonlint.com JSON确实有效(您可以在jsonlint.com上进行检查)
Your problem might be arising due to extra non whitespace characters being sent after the JSON (for example: PHP errors/warnings). 您的问题可能是由于在JSON之后发送了额外的非空格字符引起的(例如:PHP错误/警告)。 A good way to guarantee that nothing else is output after your JSON is using PHP's die
function to send content then stop executing. 保证在JSON使用PHP的die
函数发送内容然后停止执行后,不会输出任何其他东西的好方法。
die(json_encode($test));
// OR
echo json_encode($test);
die();
At the top of your PHP script, add: 在PHP脚本的顶部,添加:
header('Content-type: text/json; charset=utf-8');
If you don't have it, the server will send it as plain text, and your browser won't know that it is a json string. 如果没有它,服务器将把它作为纯文本发送,并且您的浏览器将不知道它是一个json字符串。
jQuery.get
, given the right dataType parameter (which you did) or a content-type header, does already parse the JSON for you. 给定正确的dataType参数(您已完成此操作)或内容类型标头, jQuery.get
已经为您解析了JSON。 Your callback function receives an array as the data
parameter, not a string. 您的回调函数接收一个数组作为data
参数,而不是字符串。
var json_parsed = JSON.parse(toto);
will then throw an error as toto
is not a JSON string (your FF seems to .toString()
the array, and then encounters and invalid character). 然后将引发错误,因为toto
不是JSON字符串(您的FF似乎是.toString()
数组,然后遇到无效字符)。 Instead, just use 相反,只需使用
function make_graph(toto) {
console.log(typeof toto, toto);
alert("String before : " + JSON.stringify(toto));
var json_parsed = toto; // or just use `toto` everywhere
$(document).ready(function(){
var plot1 = $.jqplot('chartdiv', json_parsed);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.