为什么json_decode对我不起作用? [英] Why json_decode doesn't work for me?

查看:272
本文介绍了为什么json_decode对我不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑.如果我将变量传递给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. 将其禁用,或者先运行$stringJSONstripslashes()使用它.

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屋!

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