PHP json_decode 不工作

标签 php json

好吧,那么,我有一个游戏可以将 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,并且由于我一直在使用 PHP 5.2,所以我无法确定使用 json_last_error 的问题是什么。

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

最佳答案

试试这个:

$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);

文档:

stripslashes - http://php.net/manual/en/function.stripslashes.php

json_decode - http://php.net/json_decode

关于PHP json_decode 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22158888/

相关文章:

android - Retrofit 2,同名不同数据类型JSON解析

PHP + MySQL "WHERE ... IN"不起作用

php - (XAMPP) Linux :MySQL said: Documentation #1932 - Table 'phpmyadmin.pma__column_info' doesn't exist in engine

javascript - Laravel 两次运行 axios 发送的请求

php - 在 php 和 MySql 中使用 highcharts 时仅显示我的标题

android - 改造 |解析嵌套的 JSON 对象(“预期为 BEGIN_ARRAY 但 BEGIN_OBJECT 位于...)

php - sql mysql查询,用于根据条件随机获取定义数量的行

javascript - Jquery 无法在 Firefox 和 IE 上执行链接点击操作

javascript - 在没有 hasOwnProperty 的情况下检查 JSON key

Ruby JSON 节点选择、查看和值更改