2013-07-29 54 views
-1

有不知道的IM應該如何把標題,但這裏是代碼:字符串不會放棄任何東西從JSON

if(isset($stats->playerStatus->server->serverIPv6Address)){ // prefer the IPv6 address if it's there`  
    $srvDetails = file_get_contents('https://api.kag2d.com/server/ip/' . $stats->playerStatus->server->serverIPv6Address . '/port/' . $stats->playerStatus->server->serverPort . '/status'); 
}elseif(isset($stats->playerStatus->server->serverIPv4Address)){ 
    $srvDetails = file_get_contents('https://api.kag2d.com/server/ip/' . $stats->playerStatus->server->serverIPv4Address . '/port/' . $stats->playerStatus->server->serverPort . '/status'); 
} 


$srvContent = json_decode($srvDetails, true); 
$srvName  = $srvContent->serverName; 
$playerString = 'Players: ' . $srvContent->currentPlayers . '/' . $srvContent->maxPlayers; 

和$ srvName保持空並且$ playerString只是玩家:/ 起飛以https://api.kag2d.com/server/ip/199.168.184.8/port/23002/status爲例 謝謝

回答

0

您未創建對象,json_decode的結果是一個數組,因爲第二個參數爲true。

刪除該參數或將訪問權限改爲數組樣式。

通過基本的調試技巧和致電var_dump($srvContent)可以很容易地發現這個錯誤。

更新 下面的代碼對我的作品:第二個轉儲的

$json = file_get_contents("https://api.kag2d.com/server/ip/199.168.184.8/port/23002/status"); 
var_dump($json); 
$decoded = json_decode($json, true); 
var_dump($decoded); 
var_dump(json_last_error()); 

結果:

array(1) { 
    'serverStatus' => 
    array(24) { 
    'DNCycle' => 
    int(0) 
    'DNState' => 
    int(0) 
    'build' => 
    int(830) 
    'connectable' => 
    int(1) 
    'currentPlayers' => 
    int(2) 
    'description' => 
    string(58) "Dreams, neon dreams, neon life, you will find, your, song." 
    'firstSeen' => 
    string(19) "2012-07-10 18:26:53" 
    'gameMode' => 
    string(15) "Team Deathmatch" 
    'gameState' => 
    int(1) 
    'gid' => 
    int(1) 
    'gold' => 
    int(1) 
    'lastUpdate' => 
    string(19) "2013-07-30 19:55:39" 
    'mapH' => 
    int(122) 
    'mapW' => 
    int(870) 
    'maxPlayers' => 
    int(10) 
    'maxSpectatorPlayers' => 
    int(0) 
    'password' => 
    int(1) 
    'playerList' => 
    array(2) { 
     [0] => 
     array(1) { 
     ... 
     } 
     [1] => 
     array(1) { 
     ... 
     } 
    } 
    'reservedPlayers' => 
    int(0) 
    'serverIPv4Address' => 
    string(13) "199.168.184.8" 
    'serverIPv6Address' => 
    string(17) "2001:470:4:727::6" 
    'serverName' => 
    string(38) "TROUBLE - Beyond - password on forums." 
    'serverPort' => 
    int(23002) 
    'spectatorPlayers' => 
    int(0) 
    } 
} 

你怎麼樣?

+0

沒有工作。 「 – Mazey

+0

」不起作用「是沒有錯誤的描述。 – Sven

+0

字符串是完全一樣的,仍然是空的 – Mazey