PHP - how to get certain data out of the JSON?

  • 1
  • Question
  • Updated 2 years ago
I am trying to get the from the json response, but after hours of trying i do not get a response.
The example works: $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};

My (partial) code:
$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'period[2]'}->{'low'}->{'celsius'};

I tried many, many variations, like:
...{'forecastday[2]'}->{'low'}->{'celsius'};
...{'forecastday'}->{2}->{'low'}->{'celsius'};
...{'forecastday'}->{[2]}->{'low'}->{'celsius'};
And dozens of other variations, based on stackoverflow feedback, internet searches, etc.

The code i found on the apicommunity.wunderground.com
$test = $data['forecast']['txt_forecast']['forecastday'][2]['fcttext'];

I know the data is here in the JSON (the -12 degrees C):

"simpleforecast":{
"forecastday":[
{
"date":{
"epoch":"1489536000",
...
...etc etc etc
...
"tz_long":"America/Chicago"
},
"period":2,
"high":{
"fahrenheit":"28",
"celsius":"-2"
},
"low":{
"fahrenheit":"10",
"celsius":"-12"

Mostly the PHP error i get is: Notice: Trying to get property of non-object
But many other errors i have seen too, like: Notice: Undefined property: stdClass::$forecastday[2]
i also tried to figure out the JSON structure with: echo '<pre>' . var_export($parsed_json, true) . '</pre>';
And i tried to  figure out the JSON with an online parser: http://json.parser.online.fr/

I just want the 10 celsius lows from the forecast10day.

Any help on how to traverse the complicated JSON is appreciated.
Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
  • confused

Posted 2 years ago

  • 1
Photo of James Anding

James Anding

  • 64 Posts
  • 49 Reply Likes
This how it should parse.

 var MyTest    = parsed_json['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'];

It works for me.
Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
Hi James, 
Thanks for your reply!

$forecast_lows = $parsed_json['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'];Gives: Fatal error: Uncaught Error: Cannot use object of type stdClass as array

$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{2}->{'celsius'};
Gives: Notice: Trying to get property of non-object

This eror i get all the time.

In the examples {} brackets are used.
This is the PHP is use:
$json_string = file_get_contents("http://api.wunderground.com/api/<my API key>/geolookup/forecast10day/q/IA/Cedar_Rapids.json"...;
$parsed_json = json_decode($json_string); # Decode the returned JSON string
Photo of Shawn

Shawn

  • 1 Post
  • 1 Reply Like
Hi Marc! This is the deal,

$forecast_lows is the value

$parsed_json is the object

and what you are looking for is the key.


For an array, it will work this way: value =object->key->key->key...


You really have two options to code this:


$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}{2}->{'low'}->{'celsius'};

OR

$forecast_lows = $parsed_json->forecast->simpleforecast->forecastday[2]->low->celsius;


For testing to figure out what data is needed, you can always run a var dump:


This is new to me as well, if you ever want to chat/skype about this, let me know I'd love someone to bounce ideas off of.


Shawn
(Edited)
Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
Hi Shawn, 

Thanks a bunch!
This part:  {'forecastday'}{2}  i had wrong all the time, i added an -> in between there also.
You saved the day.

I would like to have a sparring-partner too, i might have some valuable insights now-and-then.
Most of my contact info lives here atm: http://vegetableyard.com/about-veggies-rock.php

Regards
(Edited)