當我試圖從一個不正常的陣列呼應JSON對象。在錯誤日誌中,它給了我這個:我在做這個JSON輸出有什麼問題?
PHP Notice: Trying to get property of non-object in /home/zadmin/test.britishweb.co.uk/patchwork/featuredseries.php on line 48
對於我試圖回聲的每一個對象。這裏是我的代碼:
<?php
$source = "http://prod.cloud.rockstargames.com/global/SC/events/eventschedule-game-en.json"; // Source URL will be unchanged most likely but placed in a variable just in case.
$ch = curl_init(); // Connect
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "eventschedule-game-en.json";
$file = fopen($destination, "w+");
file_put_contents($destination, $data);
fclose($file);
$json = file_get_contents($destination);
$obj = json_decode($json, true);
var_dump($obj); // Debug option
?>
瓦爾轉儲$obj
:
{
"multiplayerEvents": [
{
"posixStartTime": 1498687200,
"posixEndTime": 1499720340,
"eventRosGameTypes": [
"gta5"
],
"eventPlatformTypes": [
"pcros",
"xboxone",
"ps4"
],
"displayName": "2x$ and RP Dawn Raid Adversary Mode",
"eventId": 20417,
"extraData": {
"eventType": "FeaturedJob"
}
}
]
}
,然後我做了HTML這樣:
<div class="main_event">
<p id="name"><?php echo $obj["displayName"]; ?> Now playing on GTA V.</p>
<h2>Selected Platforms</h2>
<p id="platforms">The series is currently running on the following platforms:</p>
<ul>
<li><?php echo $obj->eventPlatformTypes[0]; ?></li>
<li><?php echo $obj->eventPlatformTypes[1]; ?></li>
<li><?php echo $obj->eventPlatformTypes[2]; ?></li>
</ul>
</div>
你能後的var_dump($ OBJ)的'輸出;' ? – FrankerZ
'file_put_contents()'不要求你打開seperately – RiggsFolly
您的非對象錯誤是造成文件'$ obj-> eventPlatformTypes'當'obj'是一個數組 –