duan5991518
2012-10-25 19:58
浏览 135
已采纳

PHP json_decode无法解码有效的JSON

So I have this JSON object:

[
    {
        "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": []
    }
  ]

For some reason, when I try to decode it with PHP's json_decode, it doesn't output anything. The object can't be invalid, because both JSONLint and this parser parse it correctly and throw no errors. PHP's own json_last_error method doesn't throw any errors either.

I think the non-existent output might have something to do with the object having multidimensional arrays, but I'm not sure. What do you think?

EDIT

A little background here:

This JSON object is retrieved from this file. It's from an (in development) API, and this particular resource returns a JSON object from a certain "branch collection". I fetch this JSON object to PHP with cUrl, here's the code for that:

$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);

That should be also error free, because it gets the plain JSON object correctly. But when I try:

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

It just doesn't output anything, except NULL.

FINAL EDIT

Yup, this is embarassing. Found out after looking through the code, that I had accidentaly left a print_r() in the encoding process during debugging phase, which outputted the JSON object and a number 1 after it. But thanks for the replies, they were helpfull in distinguishing the problem's source!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • duangelin7513 2012-10-25 20:02
    最佳回答

    There is nothing wrong with your JSON. As you say, it parses correctly. Just so you are aware, when you "decode", nothing gets outputted. You still have to do something with it. Don't expect to see a dump after the decode.

    See here:

    http://codepad.org/LyRSQNPG

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题