0

I have the php file

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

which outputs null for both var_dumps. What is my mistake? Thanks. Is it maybe a problem that there are square brakets in the json snippet?

spooky
  • 33
  • 1
  • 4

2 Answers2

1

You have errors in your JSON.

Remove , in the end of [champions][stats] arrays.

J Price
  • 56
  • 6
0

please remove comma separation from every last element of champions->stats like below then do decode

{
    "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"
        }
    ]
}
sagar patel
  • 591
  • 5
  • 18