Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP json_解码不工作_Php_Json - Fatal编程技术网

PHP json_解码不工作

PHP json_解码不工作,php,json,Php,Json,好的,我有一个以JSON格式上传数据到我的网站的游戏。JSON与此类似: { "PlayerList":[ { "PlayerKills":0, "Username":"Player1", "TimePlayed":0, "Deaths":0 }, { "PlayerKills":0, "Username":"Player1", "TimePlayed":0

好的,我有一个以JSON格式上传数据到我的网站的游戏。JSON与此类似:

{
  "PlayerList":[
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    },
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    }
  ]
}
在确认JSON确实正确且没有错误后,我开始猜测问题在于PHP。我用来获取JSON的代码是:

$decodedJSON =  json_decode($entityBody, true, 4);              
var_dump($decodedJSON);
$entitybody作为字符串作为JSON

这里的var_dump返回NULL,因为我一直在使用PHP5.2,所以我无法确定使用json_last_error会出现什么问题


因此,如果有人能向我提供一些问题所在的信息,我们将不胜感激。

不要设置
深度
参数。只是
json\u解码($entityBody,true)应该可以工作。

在PHP5.2
json_decode
中需要2个参数,而不是3个参数。我在PHP5.2.17上进行了检查,结果显示:

Warning: json_decode() expects at most 2 parameters, 3 given. 
如果省略第三个参数,您将得到您想要的:)

尝试以下操作:

$entityBody = stripslashes($entityBody);
// this will remove all backslashes which might be the cause of returning NULL

$decodedJSON = json_decode($entityBody, true);
// leave out the depth unless you really need it to be 4.

var_dump($decodedJSON);
文件:

条带斜杠
-


json\u decode
-

似乎也在为我工作。当我的json数据格式错误时,我也遇到了这个错误(请确保以下链接中的json格式正确…..即使它看起来正确),并且我使用您更正了此错误,您肯定不应该使用PHP 5.2。它已经下线多年了,如果你在公共互联网上运行它,那么你会面临大量的安全漏洞。检查你的数据是否是带有字节顺序标记的UTF-8。我删除了我的BOM表,我的问题就消失了