简体   繁体   中英

trying to decode json file to variable but its only returning null

I'm trying to decode a json file to a PHP variable, but the PHP variable is just null. This is my PHP:

$champions['names'] = json_decode(file_get_contents("file://D:/Xampp/htdocs/lol-champions.json"),true);
echo $champions['names']['champions'][1]['name'];

This is my json file:

"champions":[
{
  "id" : 103,
  "name" : "Ahri",
},
{ 
  "id" : 84,
  "name" : "Akali",
}]

As @Paul Crovella stated, here is the correct version :

{
"champions": [{
    "id": 103,
    "name": "Ahri"
}, {
    "id": 84,
    "name": "Akali"
}]
}

Also if you want to call it;

echo $champions->champions[1]->name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM