如何使用JSON.stringify和json_de code()正确 [英] how to use JSON.stringify and json_decode() properly

查看:566
本文介绍了如何使用JSON.stringify和json_de code()正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着通过传递一个mulitidimensional JavaScript数组到另一个网页在我的网站:

Im trying to pass a mulitidimensional Javascript array to another page on my site by:


  • 阵列上透过JSON.stringify

  • using JSON.stringify on the array

的结果值分配给输入域

发布这一领域的第二页

使用json_de code上张贴的值

using json_decode on the posted value

然后的var_dump测试

then var_dump to test

(直接echo'ing张贴的变量只是为了看看它都挺过来了
在所有)

(echo'ing the posted variable directly just to see if it came through at all)

的Javascript在一个页面上:

var JSONstr = JSON.stringify(fullInfoArray);
document.getElementById('JSONfullInfoArray').value= JSONstr;

第二页上的 PHP:

$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);

echo($_POST["JSONfullInfoArray"]);

回声工作正常,但后续代码var_dump返回NULL

The echo works fine but the var_dump returns NULL

我做了什么错?

这让我搞掂:

$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

想通了使用 json_last_error()由巴特其返回sugested JSON_ERROR_SYNTAX

Figured it out using json_last_error() as sugested by Bart which returned JSON_ERROR_SYNTAX

推荐答案

您需要检查 $内容_ POST [JSONfullInfoArray] 。如果有什么不分析 json_de code 只返回。这是不是非常有帮助,所以当返回你应该检查 json_last_error()来得到了什么更多信息错了。

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.

这篇关于如何使用JSON.stringify和json_de code()正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆