2017-04-20 90 views
0

我怎麼拉76561198216468627從PHP JSON解析6

{ "response": { "steamid": "76561198216468627", "success": 1 } } 

我已經試過這一點,但它拉的1而不是ID

foreach ($parsed_json->{'reponse'} as $item) { 
$steamid = $item; 
} 
+0

您的代碼正在遍歷解析的json中的響應屬性的值。您想要直接訪問蒸汽識別號。這樣做而不是迭代。 –

回答

2

您需要的JSON字符串解碼爲一個PHP對象或數組。以下是如何各方式做到這一點:

陣列的方法

$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }'; 
$json = json_decode($jsonString, true); 
foreach($json as $item) 
{ 
    $steamid = $item['steamid']; 
} 

對象方法

$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }'; 
$json = json_decode($jsonString); 
foreach($json as $item) 
{ 
    $steamid = $item->steamid; 
} 
0

試試這個:

$json = '{ 
    "response": [{ "steamid": "76561198216468627", "success": 1 }] 
}'; 

$decode = json_decode($json); 
echo $decode->response[0]->steamid; 

然後你就可以得到76561198216468627