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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路