JSON not decoding correctly in php [duplicate]

时间:2022-10-17 11:00:50

Possible Duplicate:
Can't decode JSON string in php

可能重复:无法解码php中的JSON字符串

I'm at my wits end here, and I can't figure it out. My code was working correctly locally (using xamp) but now it won't work.

我的智慧在这里结束了,我无法理解。我的代码在本地正常工作(使用xamp),但现在它无法正常工作。

When I run this code:

当我运行此代码时:

echo "passed in parameter" . $_POST["jsoned"];
$unjasoned = json_decode("[\"23\",[],[[\"a@a.a\",\"2011-01-08\"]]]");
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);

I get:

passed in parameter[\"23\",[],[[\"a@a.a\",\"2011-01-08\"]]]
type
    array


Amount
    23

Which is exactly what i want

这正是我想要的

However the problem happens when I use the passed variable in $_POST["jsoned"] which is you see in the result above is obviously exactly the same as what im manually inserting here.

但是当我在$ _POST [“jsoned”]中使用传递的变量时会出现问题,你在上面的结果中看到的显然与我在这里手动插入的内容完全相同。

so if i do this instead (same exact input):

所以,如果我这样做(相同的确切输入):

echo "passed in parameter" . $_POST["jsoned"];
$unjasoned  = json_decode($_POST["jsoned"]);
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);

I get:

passed in parameter[\"23\",[],[[\"a@a.a\",\"2011-01-08\"]]]
type
    NULL


Amount

so...... WHAT THE HELL IS HAPPENING?! PLEASE if you have any hints share them with me, ill be eternally thankful.

所以......发生了什么事?!如果你有任何提示与我分享,那就永远感恩了。

ps. my server runs php version 5.2.13

PS。我的服务器运行php版本5.2.13

2 个解决方案

#1


1  

answered already here Can't decode JSON string in php

已经在这里回答无法解码php中的JSON字符串

#2


0  

The string in the post has \" instead of ".

帖子中的字符串有“而不是”。

When you write the string yourself as a literal you have to write \" because you're inside double quotes, but in the resulting string you'll only get a ".

当你自己写一个字符串作为文字时,你必须写“因为你在双引号内,但在结果字符串中你只会得到一个”。

Try this debug to see the difference:

试试这个调试来看看差异:

echo $_POST["jsoned"], PHP_EOL;
echo "[\"23\",[],[[\"a@a.a\",\"2011-01-08\"]]]";

#1


1  

answered already here Can't decode JSON string in php

已经在这里回答无法解码php中的JSON字符串

#2


0  

The string in the post has \" instead of ".

帖子中的字符串有“而不是”。

When you write the string yourself as a literal you have to write \" because you're inside double quotes, but in the resulting string you'll only get a ".

当你自己写一个字符串作为文字时,你必须写“因为你在双引号内,但在结果字符串中你只会得到一个”。

Try this debug to see the difference:

试试这个调试来看看差异:

echo $_POST["jsoned"], PHP_EOL;
echo "[\"23\",[],[[\"a@a.a\",\"2011-01-08\"]]]";