1

im having trouble decoding and adding this JSON data in PHP, each json contains 8 entries, after im hoping to have a site layout that 4x4 grid

JSON Structure

{
  "packages": [
    {
      "id": "TEST2362",
      "name": "TEST"
      "desc": "TEST Desc",
      "image": "URL",
      "package": "URL",
      "version": "1.00",
      "picpath": "PATH",
      "desc_1": "",
      "desc_2": "",
      "ReviewStars": "4/5 Stars",
      "Size": "1.1 GBs",
      "Author": "TESTER",
      "apptype": "TEST TYPE",
      "pv": "VER",
      "main_icon_path": "URL",
      "main_menu_pic": "PATH",
      "releaseddate": "1/1/2019"
    },

iv tried this

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$json = file_get_contents("URL.com/test.json");
$data = json_decode($json, true);

$data->packages->id[0];

echo $data;

    ?>

i get

Notice: Trying to get property 'packages' of non-object in PATH on line 10

Notice: Trying to get property 'id' of non-object in PATH on line 10

1 Answers1

1

You are trying to get the data as an object while json_decode return you an associative array.

So use $data['packages'][0]['id']; instead

Or you can change your json_decode to json_decode($json, false); The second parameter define wheter you want an object or an array.

You should be able to use it like an object like this.

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33