2017-06-13 95 views
-4

我想在我的網站上顯示API https://steamgaug.es/api/v2的一些信息。PHP JSON解碼編號

這是我當前的代碼:

$steamStatusJson = @file_get_contents("https://steamgaug.es/api/v2"); 
$steamStatus = json_decode($steamStatusJson); 
$csgoStatus = $steamStatus->ISteamGameCoordinator->730->online; 
$csgoplayerssearching = $steamStatus->ISteamGameCoordinator->730->stats->players_searching; 
$csgoplayers = $steamStatus->ISteamGameCoordinator->730->stats->players_online; 

我總是收到此錯誤信息:

致命錯誤語法錯誤,意想不到的 '730'(T_LNUMBER),預計標識符(T_STRING)或可變

回答

1

的孤獨,你作爲一個對象解碼的JSON,你不能用數字作爲properties names

所以,你需要這條線:

$csgoStatus = $steamStatus->ISteamGameCoordinator->730->online; 

應該如下:

$csgoStatus = $steamStatus->ISteamGameCoordinator->{"730"}->online; 
//             ^^^^^^^ 

也同樣與那些臺詞:

$csgoplayerssearching = $steamStatus->ISteamGameCoordinator->{"730"}->stats->players_searching; 
$csgoplayers = $steamStatus->ISteamGameCoordinator->{"730"}->stats->players_online; 

,或者乾脆通過解碼你的json作爲陣列

$steamStatus = json_decode($steamStatusJson, true); 

,然後您可以訪問它爲:

$csgoStatus = $steamStatus['ISteamGameCoordinator']['730']['online']; 
//.... 
+0

完美,非常感謝你 – Enge