json_decode不对我的变量起作用,但是在其内容上(json_decode is not working on my variable but is on its content)

我有以下代码:

$param = $params[0]; var_dump($param->getValue()); $test = json_decode($param->getValue()); var_dump($test);

我的第一个var_dump返回以下内容:

string(133) ""[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]""

第二个返回以下内容:

string(107) "[{"lang_id": "1", "naam": "dsfsdfds", "mail": "dsfdsfs"}, {"lang_id": "1", "naam": "dfsd", "mail": "dfds"}]"

并将值保存在我的DB中,如下所示:

"[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]"

现在我的问题是:为什么它在json_decode之后返回一个字符串? 我完全不知道我做错了什么,最奇怪的是如果我用该变量的实际值替换变量,那么解码是正确的:

$test = json_decode("[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]");

回报

array(2) { [0]=> object(stdClass)#3255 (3) { ["lang_id"]=> string(1) "1" ["naam"]=> string(8) "dsfsdfds" ["mail"]=> string(7) "dsfdsfs" } [1]=> object(stdClass)#3256 (3) { ["lang_id"]=> string(1) "1" ["naam"]=> string(4) "dfsd" ["mail"]=> string(4) "dfds" } }

我究竟做错了什么?

I have the following piece of code:

$param = $params[0]; var_dump($param->getValue()); $test = json_decode($param->getValue()); var_dump($test);

my first var_dump returns the following:

string(133) ""[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]""

the seconde one is returning the following:

string(107) "[{"lang_id": "1", "naam": "dsfsdfds", "mail": "dsfdsfs"}, {"lang_id": "1", "naam": "dfsd", "mail": "dfds"}]"

and the value is saved in my DB like this:

"[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]"

Now my question is: Why is it returning a string after the json_decode? I have absolutely no idea what i'm doing wrong and the strangest thing is that if i replace the variable with the actual value of that variable then the decode is correct:

$test = json_decode("[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]");

returns

array(2) { [0]=> object(stdClass)#3255 (3) { ["lang_id"]=> string(1) "1" ["naam"]=> string(8) "dsfsdfds" ["mail"]=> string(7) "dsfdsfs" } [1]=> object(stdClass)#3256 (3) { ["lang_id"]=> string(1) "1" ["naam"]=> string(4) "dfsd" ["mail"]=> string(4) "dfds" } }

What am i doing wrong?

最满意答案

因为你的字符串是json_encoded两次,所以你需要解码它两次。

string(133) ""[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]""

如果您查看上面的字符串,您会看到所有引号都被转义,并且在开头和结尾都有双引号。 所以这意味着如果你json解码你得到一个没有转义引号的字符串。

如果再次解码,字符串将被解码为数组。

json_decode('"[12,24,32]"'); //php string: [12,24,32] json_decode('[12,24,32]'); //php array(12, 24, 32);

because your string is json_encoded twice, so you need to decode it twice.

string(133) ""[{\"lang_id\": \"1\", \"naam\": \"dsfsdfds\", \"mail\": \"dsfdsfs\"}, {\"lang_id\": \"1\", \"naam\": \"dfsd\", \"mail\": \"dfds\"}]""

if you look in the string above you see that all he quotes are escaped and there is a double quote in the beginning and in the end. so this means if you json decode you get an string withouth the escaped quotes.

if you decode again the string will be decoded to an array.

json_decode('"[12,24,32]"'); //php string: [12,24,32] json_decode('[12,24,32]'); //php array(12, 24, 32);

更多推荐