2015-10-21 49 views
-2

因此,这里是jsonfile解析JSON后的输出为阵列()

我需要在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是上面代码的输出

谁能告诉我什么是错误?

+0

删除[ '阿凡达']从$播放器[ '阿凡达'] [ '用户名'] –

+1

看起来json_decode()想要JSON,但你给它一个URL。首先获取JSON数据,然后将结果传递给json_parse()。 –

+0

我试过了,但它仍然不能正常工作。 – user3452599

回答

1
$arr = json_decode($a, TRUE); 

从URL,而不是URL的内容解码JSON(你的解码{id:....http://.....代替)

因此,相反,你需要得到位于URL另一端的数据。

因此改变该行:

$arr = json_decode(file_get_contents($a), TRUE);