为什么json_decode对我不起作用? [英] Why json_decode doesn't work for me?
问题描述
我有点困惑.如果我将变量传递给json_decode,它将不起作用:
I'm a little confused here. if I pass a variable to json_decode, it doesn't work:
$stringJSON = $_GET['jsonstring'];
echo $stringJSON;
$stringObject = json_decode($stringJSON);
var_export($stringObject);
第一个回显正确地显示了我传递的JSON字符串,例如
The first echo correctly shows me the JSON string I passed, e.g.
{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}
第二个回显显示NULL. 因此,我从第一个回显中获取了字符串,并编写了以下代码:
The second echo shows NULL. So I grab the string from the first echo and write the following code:
$stringObject = json_decode("{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}");
var_export ($stringObject);
您怎么说,它向我展示了正确解码的数组.字符串绝对相同,我什至保留转义字符.也许是问题所在?
And what do you say, it shows me the correctly decoded array. The string is absolutely the same, I even kept the escape characters. Or maybe they are the problem?
推荐答案
好像您的服务器已启用magic_quotes_gpc
. 将其禁用,或者先运行$stringJSON
到stripslashes()
使用它.
Looks like your server has magic_quotes_gpc
enabled. Either disable it or run $stringJSON
through stripslashes()
before using it.
$stringJSON = get_magic_quotes_gpc() ?
stripslashes($_GET['jsonstring']) : $_GET['jsonstring'];
这篇关于为什么json_decode对我不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!