好了,我有一个游戏,它以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出了什么问题。
因此,如果有人能提供一些关于问题所在的信息,我将不胜感激。
发布于 2014-03-04 07:16:02
试试这个:
$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
发布于 2014-03-04 06:30:55
不要设置depth参数。只要json_decode($entityBody,true);就行了。
发布于 2014-03-04 06:42:52
在PHP5.2中,json_decode需要2个参数,而不是3个。我在PHP5.2.17上检查了一下,结果显示:
Warning: json_decode() expects at most 2 parameters, 3 given. 如果你省略了第三个参数,你会得到你想要的:)
https://stackoverflow.com/questions/22158888
复制相似问题