从 $_POST 中的 json 读取关联数组 [英] Read associative array from json in $_POST
本文介绍了从 $_POST 中的 json 读取关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 jQuery 将 json 对象发布到我的 php 应用程序.
I am using jQuery to post a json object to my php application.
jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });
从 firebug 中提取的 json 字符串如下所示
The json string as pulled from firebug looks like this
{ "data" : [ { "contents" : "This is some content",
"selector" : "DIV.subhead"
},
{ "contents" : "some other content",
"selector" : "LI:nth-child(1) A"
}
],
"page" : "about_us.php"
}
在 php 中,我试图把它变成一个关联数组.
In php I am trying to turn this into an associative array.
到目前为止我的 php 代码是
My php code so far is
<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>
对 ajax 调用的响应应该是about_us.php",但返回空白.
The response to the ajax call should be "about_us.php" but it comes back blank.
推荐答案
你可以避免使用JSON.stringify
和json_decode
:
jQuery.post("save.php", dataToSend, function(data){ alert(data); });
还有:
<?php
echo $_POST['page'];
?>
更新:
...但如果你真的想使用它们,那么:
... but if your really want to use them, then:
jQuery.post("save.php", {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });
还有:
<?php
$value = json_decode($_POST['json']);
echo $value->page;
?>
这篇关于从 $_POST 中的 json 读取关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文