php 为什么 json_decode 对我不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8904764/
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
Why json_decode doesn't work for me?
提问by taralex
I'm a little confused here. if I pass a variable to json_decode, it doesn't work:
我在这里有点困惑。如果我将变量传递给 json_decode,则它不起作用:
$stringJSON = $_GET['jsonstring'];
echo $stringJSON;
$stringObject = json_decode($stringJSON);
var_export($stringObject);
The first echo correctly shows me the JSON string I passed, e.g.
第一个回声正确地向我显示了我传递的 JSON 字符串,例如
{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}
The second echo shows NULL. So I grab the string from the first echo and write the following code:
第二个回声显示 NULL。所以我从第一个 echo 中获取字符串并编写以下代码:
$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?
你说什么,它向我展示了正确解码的数组。字符串完全相同,我什至保留了转义字符。或者他们可能是问题所在?
回答by Phil
Looks like your server has magic_quotes_gpc
enabled. Either disable itor run $stringJSON
through stripslashes()
before using it.
看起来您的服务器已magic_quotes_gpc
启用。无论是将其禁用或运行$stringJSON
通过stripslashes()
使用它之前。
$stringJSON = get_magic_quotes_gpc() ?
stripslashes($_GET['jsonstring']) : $_GET['jsonstring'];
回答by Joe
This
这个
[{\"Name\":\"name\",\"Description\":\"\"]
needs to be
需要是
[{\"Name\":\"name\",\"Description\":\"\"}]
You are missing the closing }
你错过了结束 }
回答by Eugen Rieck
This is a quoting problem: Try the following
这是一个引用问题:请尝试以下操作
$stringObject = json_decode("{\"Items\":[{\"Name\":\"name\",\"Description\":\"\"],\"Name\":\"Christmas\"}");
echo $stringObject;
var_export ($stringObject);
as you see, the $stringObject has no quotes (but the one coming from $_GET has them)
如您所见, $stringObject 没有引号(但来自 $_GET 的有引号)
so you might need
所以你可能需要
$stringJSON = $_GET['jsonstring'];
$stringObject = json_decode(stripslashes($stringJSON));
var_export($stringObject);
回答by deceze
If it shows you a string with slashes in it when you echo
it, that means the string has slashes in it. That's not a valid JSON string, the slashes don't belong there. If you paste that string into PHP, the slashes are evaluated by PHP. The string literal "\""
in PHP source code evaluates to the string "
, so the slashes are effectively removed and you are decoding a valid JSON string.
如果它在您输入时向您显示一个带有斜线echo
的字符串,则表示该字符串中有斜线。这不是有效的 JSON 字符串,斜线不属于那里。如果将该字符串粘贴到 PHP 中,则 PHP 会评估斜线。"\""
PHP 源代码中的字符串文字计算结果为 string "
,因此斜线被有效删除,您正在解码有效的 JSON 字符串。
I suspect you have Magic Quoteson which are inserting the slashes into GET values, turn them off.
我怀疑您有魔术引号将斜杠插入到 GET 值中,将它们关闭。
回答by Mohamed Adel El-Badry
run json_decode twice.
运行 json_decode 两次。
$str = json_decode($jsonData,true);
$str = json_decode($str,true);
It may help someone.
它可能会帮助某人。
回答by Nithee
json_encode($str, JSON_UNESCAPED_SLASHES);
it may help you.
它可能会帮助你。