php 如何正确使用 JSON.stringify 和 json_decode()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15986235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use JSON.stringify and json_decode() properly
提问by Wesley Smith
Im trying to pass a mulitidimensional Javascript array to another page on my site by:
我试图通过以下方式将多维 Javascript 数组传递到我网站上的另一个页面:
using JSON.stringify on the array
assigning the resultant value to an input field
posting that field to the second page
using json_decode on the posted value
then var_dump to test
(echo'ing the posted variable directly just to see if it came through at all)
在数组上使用 JSON.stringify
将结果值分配给输入字段
将该字段发布到第二页
在发布的值上使用 json_decode
然后 var_dump 进行测试
(直接回显已发布的变量只是为了查看它是否通过)
Javascript on page one:
第一页的 Javascript:
var JSONstr = JSON.stringify(fullInfoArray);
document.getElementById('JSONfullInfoArray').value= JSONstr;
php on page two:
php 在第二页:
$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);
echo($_POST["JSONfullInfoArray"]);
The echo works fine but the var_dump returns NULL
回声工作正常,但 var_dump 返回 NULL
What have I done wrong?
我做错了什么?
This got me fixed up:
这让我解决了:
$postedData = $_POST["JSONfullInfoArray"];
$tempData = str_replace("\", "",$postedData);
$cleanData = json_decode($tempData);
var_dump($cleanData);
Im not sure why but the post was coming through with a bunch of "\" characters separating each variable in the string
我不知道为什么,但帖子通过一堆“\”字符分隔字符串中的每个变量
Figured it out using json_last_error()
as sugested by Bart which returned JSON_ERROR_SYNTAX
使用json_last_error()
返回的 Bart 的 sugested想通了JSON_ERROR_SYNTAX
采纳答案by Bart
You'll need to check the contents of $_POST["JSONfullInfoArray"]
. If something doesn't parse json_decode
will just return null
. This isn't very helpful so when null
is returned you should check json_last_error()
to get more info on what went wrong.
您需要检查$_POST["JSONfullInfoArray"]
. 如果某些东西没有解析json_decode
将返回null
。这不是很有帮助,因此何时null
返回您应该检查json_last_error()
以获取有关问题的更多信息。
回答by user1881928
When you use JSON stringify then use html_entity_decode first before json_decode.
当您使用 JSON stringify 时,请在 json_decode 之前先使用 html_entity_decode。
$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);
回答by S. A. Malik
When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.
当您使用 JSON.stringify() 保存一些数据然后需要在 php 中读取它时。以下代码对我有用。
json_decode( html_entity_decode( stripslashes ($jsonString ) ) );
Thanks to @Thisguyhastwothumbs
感谢@Thisguyhastwothumbs
回答by LOVEPIN
stripslashes(htmlspecialchars(JSON_DATA))
回答by Yuv
None of the other answers worked in my case, most likely because the JSON array contained special characters. What fixed it for me:
在我的情况下,其他答案都不起作用,很可能是因为 JSON 数组包含特殊字符。什么为我修好了:
Javascript (added encodeURIComponent)
Javascript(添加了 encodeURIComponent)
var JSONstr = encodeURIComponent(JSON.stringify(fullInfoArray));
document.getElementById('JSONfullInfoArray').value = JSONstr;
PHP (unchanged from the question)
PHP(与问题保持不变)
$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);
echo($_POST["JSONfullInfoArray"]);
Both echo and var_dump have been verified to work fine on a sample of more than 2000 user-entered datasets that included a URL field and a long text field, and that were returning NULL on var_dump for a subset that included URLs with the characters ?&#
.
echo 和 var_dump 都经过验证,可以在包含 URL 字段和长文本字段的 2000 多个用户输入数据集的样本上正常工作,并且在 var_dump 上为包含带有字符的 URL 的子集返回 NULL ?&#
。
回答by KRISHNA KANT SINGH
jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);`
回答by user10971804
I don't how this works, but it worked.
我不知道这是如何工作的,但它确实有效。
$post_data = json_decode(json_encode($_POST['request_key']));