在PHP中解码JSON对象数组,是反斜杠吗?
因此,我使用javascript将JSON字符串中的对象数组发布到PHP脚本中,在PHP中解码它时遇到了实际问题 我的javascript如下所示:在PHP中解码JSON对象数组,是反斜杠吗?,php,jquery,json,Php,Jquery,Json,因此,我使用javascript将JSON字符串中的对象数组发布到PHP脚本中,在PHP中解码它时遇到了实际问题 我的javascript如下所示: $.ajax({ type: 'POST', url: "question_save.php", data: {myJson: JSON.stringify(jsonArray)}, success: function(data){ alert(data); } }); 发送到PHP的
$.ajax({
type: 'POST',
url: "question_save.php",
data: {myJson: JSON.stringify(jsonArray)},
success: function(data){
alert(data);
}
});
发送到PHP的字符串如下所示:
[{"content":"Question text"},{"answerContent":"Some answer text","score":"234","responseChecked":0,"responseContent":""},{"answerContent":"","score":"0","responseChecked":0,"responseContent":""}]
如果我回显$u POST['myJson'],我会得到以下结果:
[{\"content\":\"Question text\"},{\"answerContent\":\"Some answer text\",\"score\":\"234\",\"responseChecked\":0,\"responseContent\":\"\"},{\"answerContent\":\"\",\"score\":\"0\",\"responseChecked\":0,\"responseContent\":\"\"}]
但是当我想解码JSON并像这样循环时
$json = $_POST['myJson'];
$data = json_decode($json, true);
foreach ($data as &$value) {
echo("Hi there");
}
…我得到这个错误:
Warning: Invalid argument supplied for foreach() in /home/thecrime/public_html/test1/question_save.php on line 15
我真的不明白我犯了什么愚蠢的错误,是不是和背部的伤口有关
非常感谢任何帮助
谢谢,
-Ben使用斜杠($string)
-
这应该行得通
$json = $_POST['myJson'];
$json_string = stripslashes($json)
$data = json_decode($json_string, true);
// simple debug
echo "<pre>";
print_r($data);
将其设置为关闭,然后重新启动服务器。这与最好禁用这个恼人的旧功能,忘掉那些问题。
您可以禁用以下内容 打印$data值,在json_解码后查看此变量的值…Simone你是我的救世主,我爱你。谢谢尽管这显然是一个有效的答案,但这是对抗症状而不是原因:magic_quotes_gpc。关掉它们,你就再也不用担心这样的事情了。
magic_quotes_gpc = On