PHP 的json_decode无法解码有效的 JSON



所以我有这个json对象:

[
    {
        "branch_id": "1",
        "issue_id": "1",
        "user_id": "5",
        "parent_id": null,
        "level": "1",
        "name": "troll",
        "description": "yup",
        "add_date": "2012-10-24 20:26:04",
        "children": [
            {
                "branch_id": "2",
                "issue_id": "1",
                "user_id": "5",
                "parent_id": "1",
                "level": "2",
                "name": "sdad",
                "description": "dssfsd",
                "add_date": "2012-10-24 20:52:52",
                "children": [
                    {
                        "branch_id": "4",
                        "issue_id": "1",
                        "user_id": "5",
                        "parent_id": "2",
                        "level": "3",
                        "name": "fdgffd",
                        "description": "ghjjhjghjj",
                        "add_date": "2012-10-25 17:51:53",
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "branch_id": "3",
        "issue_id": "1",
        "user_id": "5",
        "parent_id": null,
        "level": "1",
        "name": "dgdfg",
        "description": "dfgfgdfg",
        "add_date": "2012-10-24 20:52:52",
        "children": []
    }
  ]

由于某种原因,当我尝试用PHP的JSON_DECODE解码时,它不会输出任何内容。该对象不会无效,因为JSONLINT和此解析器都正确解析并没有抛出任何错误。PHP自己的JSON_LAST_ERROR方法也不会丢任何错误。

我认为不存在的输出可能与具有多维数组的对象有关,但我不确定。你怎么看?

编辑

这里有一点背景:

从此文件中检索此JSON对象。它来自(开发)API,该特定资源从某个"分支集合"中返回JSON对象。我将此JSON对象带到卷发上,这是为此的代码:

$ch = curl_init('http://skibb.it/api/issues/branches?issue_id=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$branches = curl_exec($ch);
curl_close($ch);

也应该没有错误,因为它可以正确获取普通的JSON对象。但是当我尝试时:

$branches = json_decode($branches);
var_dump($branches); //Or print_r($branches);

它只是没有输出任何东西,除了空。

最终编辑

是的,这很尴尬。在查看代码后发现,我偶然地在调试阶段的编码过程中留下了一个print_r(),该过程输出了JSON对象,并且在IT 之后输出了数字1。但是感谢您的答复,他们有助于区分问题的来源!

您的JSON没有错。正如您所说,它正确解析了。这样您就知道,当您"解码"时,什么都不会输出。您仍然必须对此做些事情。解码后不要期望看到转储。

请参阅此处:

http://codepad.org/lyrsqnpg

这是示例JSON解码。

$jsonDayArray    = $_REQUEST['jsonDayArray'];
    $jsonDayArray    = str_replace("\","",$jsonDayArray);
    $DayArray = array();
    $DayArray = json_decode($jsonDayArray, true);

最新更新