我在解码JSON时遇到问题

时间:2019-10-10 02:36:49

标签: php json decode

$ test2 = json_decode($ data,true);

print_r($ test2 ['statistics'] ['Vitalité'] [0] ['max']);

print_r($ test2 ['statistics'] ['Chance'] [0] ['max']);

print_r($ test2 ['statistics'] ['Agilité'] [0] ['max']);

print_r($ test2 ['name']);

2 个答案:

答案 0 :(得分:1)

因为您为变量设置了错误的键。正确的代码:

$data = file_get_contents("https://fr.dofus.dofapi.fr/equipments/13644");

$test2 = json_decode($data, true);

//var_dump("<pre>", $test2['statistics'], "</pre>");

print_r($test2['statistics'][0]['Vitalité']['max']);

print_r($test2['statistics'][1]['Chance']['max']);

print_r($test2['statistics'][2]['Agilité']['max']);

print_r($test2['name']);

答案 1 :(得分:0)

  • 有一种仅用数字[0]代替['statistics'] [0]的解决方案

我的代码

$ sub = json_decode($ data,true);

$ vita = $ sub ['statistics'] [0] ['Vitalité'] ['max'];

$ chance = $ sub ['statistics'] [1] ['Chance'] ['max'];

$ agil = $ sub ['statistics'] [2] ['Agilité'] ['max'];

$ sag = $ sub ['statistics'] [3] ['Sagesse'] ['max'];