简体   繁体   中英

What am I doing wrong with this JSON output?

When I am trying to echo the JSON objects from the array it is not working. In the error logs it gives me this:

PHP Notice:  Trying to get property of non-object in /home/zadmin/test.britishweb.co.uk/patchwork/featuredseries.php on line 48

For every one of the objects I'm trying to echo. Here is my code:

<?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

?>

Var dump of $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"
      }
    }
  ]
}

and then I am doing the HTML as such:

<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>

You're attempting to access $obj as an object and not an array. The fact that you're passing true to the second parameter of json_decode() , tells you that the object returned is an array and not an object. Access properties using the same way you accessed them on this line:

<p id="name"><?php echo $obj["displayName"]; ?> Now playing on GTA V.</p>

Also, be aware there is no "displayName" on $obj . It's a part of the multiplayerEvents array. So, access your $obj array like so:

<div class="main_event">
    <p id="name"><?php echo $obj["multiplayerEvents"][0]["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["multiplayerEvents"][0]["eventPlatformTypes"][0]; ?></li>
        <li><?php echo $obj["multiplayerEvents"][0]["eventPlatformTypes"][1]; ?></li>
        <li><?php echo $obj["multiplayerEvents"][0]["eventPlatformTypes"][2]; ?></li>

    </ul>
</div>

One other slight note. You may want to iterate through your eventPlatformTypes, (And maybe even your multiplerEvents as well) in case there are a variable number of types/events:

    <ul>
        <?php foreach ( $obj["multiplayerEvents"][0]["eventPlatformTypes"] as $platformType ): ?>
        <li><?php echo $platformType; ?></li>
        <?php endforeach; ?>
    </ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM