json文件到php数组。使用json_decode给出null

时间:2017-02-01 11:37:17

标签: php json

我有php文件

<?php
$str = '{
    "champions": [{
        "id": 24,
        "stats": {
            "armor": 27.04,
            "attackrange": 125.0,
        }
    }, {
        "id": 37,
        "stats": {
            "armor": 20.544,
            "attackrange": 550.0,

        }
    }],
    "matches": [{
        "timestamp": 1433644800,
        "champion": 427,
        "lane": "TOP"
    }, {"timestamp": 1453702800,
        "champion": 103,
        "lane": "MIDDLE"
    }]
}';

  $array = json_decode($str,true);// read string to array (true means array, false means object)
  var_dump($array);
  $champions = $array["champions"];
  var_dump($champions);

为var_dumps输出null。我的错是什么?谢谢。是不是json片段中有方形支架的问题?

2 个答案:

答案 0 :(得分:1)

您的JSON中有错误。

删除,数组末尾的[champions][stats]

答案 1 :(得分:0)

请从冠军的每个最后一个元素中移除逗号分隔 - &gt;如下所示的统计数据然后执行解码

{
    "champions": [
        {
            "id": 24,
            "stats": {
                "armor": 27.04,
                "attackrange": 125
            }
        },
        {
            "id": 37,
            "stats": {
                "armor": 20.544,
                "attackrange": 550
            }
        }
    ],
    "matches": [
        {
            "timestamp": 1433644800,
            "champion": 427,
            "lane": "TOP"
        },
        {
            "timestamp": 1453702800,
            "champion": 103,
            "lane": "MIDDLE"
        }
    ]
}