PHP JSON解码编号
问题描述:
我想在我的网站上显示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)或可变
答
的孤独,你作为一个对象解码的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'];
//....
完美,非常感谢你 – Enge