在解析JSON之后,输出是array()

时间:2015-10-21 13:06:22

标签: php html json parsing

所以这是json file

我需要在标题标记内的 html 页面中显示namescorewarsWonWarsLost

我需要在 html 表格中显示所有玩家的userNamerolelevel

我使用以下代码,但输出为array()

<?php
$a = 'http://185.112.249.77:9999/Api/Clan?clan=274879547254'; // place your JSON here. If string, add signle quotes around it.

$arr = json_decode($a, TRUE);

$names = $arr['name'];
$score = $arr['score'];
$warsWon = $arr['warsWon'];
$warsLost = $arr['warsLost'];

$users = array();
if (! empty($arr['players'])) {
  foreach ($arr['players'] as $player) {

    $users[$player['avatar']['userId']]['userName'] = $player['avatar']['userName'];
    $users[$player['avatar']['userId']]['role'] = $player['avatar']['role'];
    $users[$player['avatar']['userId']]['level'] = $player['avatar']['level'];
  }
}
 echo '<pre>';
print_r($users);
echo '</pre>';

?>

Here是上述代码的输出

谁能告诉我这是什么错误?

1 个答案:

答案 0 :(得分:1)

$arr = json_decode($a, TRUE);

正在解析网址的字符串中的JSON,而不是网址的内容(您正在解码http://.....而非{{ 1}})

因此,您需要获取URL另一侧的数据。

所以将该行更改为:

{id:....