PHP -> strJsonFile -> json_decode

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
gcclinux
New php-forum User
New php-forum User
Posts: 1
Joined: Sun Aug 25, 2019 10:59 am

Sun Aug 25, 2019 11:07 am

Hello,

I am trying to reach individual objects in this JSON file but no matter what I do I can not get a single individual item only var_dump($obj); works

Example from var_dump output....

Code: Select all

{
  "258": {
    "AT": {
      "av": -65.532, 
      "ct": 296965, 
      "mn": -100.044, 
      "mx": -26.219
    }, 
    "First_UTC": "2019-08-18T07:24:23Z", 
    "HWS": {
      "av": 4.745, 
      "ct": 130562, 
      "mn": 0.18600000000000003, 
      "mx": 16.788
    }, 
    "Last_UTC": "2019-08-19T08:03:59Z", 
    "PRE": {
      "av": 758.968, 
      "ct": 147696, 
      "mn": 741.6062, 
      "mx": 778.9984
    }, 
    "Season": "winter", 
    "WD": {
      "1": {
        "compass_degrees": 22.5, 
        "compass_point": "NNE", 
        "compass_right": 0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 1
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 12588
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 8803
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 13883
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 146
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 2329
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 3116
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 24963
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 27884
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 13003
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 23846
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 27884
      }
    }
  }, 
Lets say I want to get First_UTC & Last_UTC how do I do that?

Here is my entire script so far.

Code: Select all

#!/usr/bin/php

<?php 

// Initialize a file URL to the variable 
$url = 'https://mars.nasa.gov/rss/api/?feed=weather&category=insight&feedtype=json'; 
  
// Initialize the cURL session 
$ch = curl_init($url); 
  
// Inintialize directory name where 
// file will be save 
$dir = './'; 
  
// Use basename() function to return 
// the base name of file  
// $file_name = basename($url); 

$file_name = 'rawdata.json';
  
// Save file into file location 
$save_file_loc = $dir . $file_name; 
  
// Open file  
$fp = fopen($save_file_loc, 'wb'); 
  
// It set an option for a cURL transfer 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
  
// Perform a cURL session 
curl_exec($ch); 
  
// Closes a cURL session and frees all resources 
curl_close($ch); 
  
// Close file 
fclose($fp); 

$strJsonFileContents = file_get_contents($save_file_loc); 
$obj = json_decode($strJsonFileContents);

var_dump($obj); 

?> 

Any help is greatly appreciated,
Many Thanks
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 859
Joined: Mon Feb 22, 2016 5:52 pm

Tue Aug 27, 2019 8:59 am

You need to set json_decode $assoc to true

Code: Select all

<?php

$file = '{
    "258": {
      "AT": {
        "av": -65.532,
        "ct": 296965,
        "mn": -100.044,
        "mx": -26.219
      },
      "First_UTC": "2019-08-18T07:24:23Z",
      "HWS": {
        "av": 4.745,
        "ct": 130562,
        "mn": 0.18600000000000003,
        "mx": 16.788
      },
      "Last_UTC": "2019-08-19T08:03:59Z",
      "PRE": {
        "av": 758.968,
        "ct": 147696,
        "mn": 741.6062,
        "mx": 778.9984
      },
      "Season": "winter",
      "WD": {
        "1": {
          "compass_degrees": 22.5,
          "compass_point": "NNE",
          "compass_right": 0.382683432365,
          "compass_up": 0.923879532511,
          "ct": 1
        },
        "10": {
          "compass_degrees": 225.0,
          "compass_point": "SW",
          "compass_right": -0.707106781187,
          "compass_up": -0.707106781187,
          "ct": 12588
        },
        "11": {
          "compass_degrees": 247.5,
          "compass_point": "WSW",
          "compass_right": -0.923879532511,
          "compass_up": -0.382683432365,
          "ct": 8803
        },
        "12": {
          "compass_degrees": 270.0,
          "compass_point": "W",
          "compass_right": -1.0,
          "compass_up": -0.0,
          "ct": 13883
        },
        "2": {
          "compass_degrees": 45.0,
          "compass_point": "NE",
          "compass_right": 0.707106781187,
          "compass_up": 0.707106781187,
          "ct": 146
        },
        "3": {
          "compass_degrees": 67.5,
          "compass_point": "ENE",
          "compass_right": 0.923879532511,
          "compass_up": 0.382683432365,
          "ct": 2329
        },
        "5": {
          "compass_degrees": 112.5,
          "compass_point": "ESE",
          "compass_right": 0.923879532511,
          "compass_up": -0.382683432365,
          "ct": 3116
        },
        "6": {
          "compass_degrees": 135.0,
          "compass_point": "SE",
          "compass_right": 0.707106781187,
          "compass_up": -0.707106781187,
          "ct": 24963
        },
        "7": {
          "compass_degrees": 157.5,
          "compass_point": "SSE",
          "compass_right": 0.382683432365,
          "compass_up": -0.923879532511,
          "ct": 27884
        },
        "8": {
          "compass_degrees": 180.0,
          "compass_point": "S",
          "compass_right": 0.0,
          "compass_up": -1.0,
          "ct": 13003
        },
        "9": {
          "compass_degrees": 202.5,
          "compass_point": "SSW",
          "compass_right": -0.382683432365,
          "compass_up": -0.923879532511,
          "ct": 23846
        },
        "most_common": {
          "compass_degrees": 157.5,
          "compass_point": "SSE",
          "compass_right": 0.382683432365,
          "compass_up": -0.923879532511,
          "ct": 27884
        }
      }
    }
  }';

$json = json_decode($file, true);

echo $json['258'] ['First_UTC'];
Check out the manual so that you can access the rest of the array.
Post Reply