因此,我發佈了一個JSON字符串中的對象數組使用JavaScript到PHP腳本,我有真正的問題在PHP解碼。在PHP中解碼JSON數組對象,它是反斜槓嗎?
我的JavaScript如下:
$.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":""}]
如果我回聲$ _ 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
打印$ data值,看看json_decode後這個變量的值是多少... – Dev