首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP json_decode不工作

问PHP json_decode不工作
EN

Stack Overflow用户
提问于 2014-03-04 06:29:22
回答 3查看 12.7K关注 0票数 3

好了,我有一个游戏,它以JSON的形式将数据上传到我的站点。JSON类似于:

代码语言:javascript
复制
{
  "PlayerList":[
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    },
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    }
  ]
}

在确认JSON确实正确并且没有错误之后,我开始猜测问题出在PHP上。我用来获取JSON的代码是:

代码语言:javascript
复制
$decodedJSON =  json_decode($entityBody, true, 4);              
var_dump($decodedJSON);

其中$entitybody是字符串形式的JSON。

这里的var_dump返回NULL,因为我坚持使用PHP5.2,所以我不能确定使用json_last_error出了什么问题。

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

EN

回答 3

Stack Overflow用户

发布于 2014-03-04 07:16:02

试试这个:

代码语言:javascript
复制
$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

票数 7
EN

Stack Overflow用户

发布于 2014-03-04 06:30:55

不要设置depth参数。只要json_decode($entityBody,true);就行了。

票数 4
EN

Stack Overflow用户

发布于 2014-03-04 06:42:52

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

代码语言:javascript
复制
Warning: json_decode() expects at most 2 parameters, 3 given. 

如果你省略了第三个参数,你会得到你想要的:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22158888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档