PHP   发布时间:2022-04-06  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP json_decode不工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

好吧,所以,我有一个游戏,以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不工作全部内容,希望文章能够帮你解决PHP json_decode不工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: