json_decode的print_r为空

时间:2018-04-10 17:29:12

标签: php json

我有一个看起来像

的文件data.json
{
"18.01": [{
        "name": "Title 1",
        "id": "0",
        "content": {
            "html": "This should be 18.01 video 1",
            "link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
        },
    }, {
        "name": "test_video_2",
        "id": "1",
        "content": {
            "html": "This should be 18.01 video 2",
            "link": "https://www.youtube.com/embed/KWafZrA5OBc"
        },
    }],
    "18.02": [{
        "name": "test_video_3",
        "id": "0",
        "content": {
            "html": "This should be 18.02 video 3",
            "link": "https://www.youtube.com/embed/EMELKZhM2-g"
        },
    }, {
        "name": "test_video_4",
        "id": "1",
        "content": {
            "html": "This should be 18.02 video 4",
            "link": "https://www.youtube.com/embed/TkWmK-cplV4"
        },
    }]
}

然后我在同一个文件夹中有一个php文件:

<?php

//Decode JSON
$json_data = json_decode(file_get_contents('data.json'), TRUE);

//Print data
print_r($json_data);

?>

但是当我在浏览器中加载这个php文件时,我得到一个空白页面。我的php服务正在运行(其他php工作正常),如果我只打印我正在加载的文件,它显示为字符串。

编辑:如果您收到此错误,请记住验证您的JSON与我不同...

1 个答案:

答案 0 :(得分:1)

上面的JSON在语法上是错误的。有效的JSON是:

    {
  "18.01": [
    {
      "name": "Title 1",
      "id": "0",
      "content": {
        "html": "This should be 18.01 video 1",
        "link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
      }
    },
    {
      "name": "test_video_2",
      "id": "1",
      "content": {
        "html": "This should be 18.01 video 2",
        "link": "https://www.youtube.com/embed/KWafZrA5OBc"
      }
    }
  ],
  "18.02": [
    {
      "name": "test_video_3",
      "id": "0",
      "content": {
        "html": "This should be 18.02 video 3",
        "link": "https://www.youtube.com/embed/EMELKZhM2-g"
      }
    },
    {
      "name": "test_video_4",
      "id": "1",
      "content": {
        "html": "This should be 18.02 video 4",
        "link": "https://www.youtube.com/embed/TkWmK-cplV4"
      }
    }
  ]
}
  

在JSON中不允许使用尾随逗号,就像在问题中提到的JSON字符串第8行

此外,要验证JSON,您可以使用此nice website