好吧,那么,我有一个游戏可以将 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/