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

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条)

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据