• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PHP / JSON displaying nested objects

Question

Laser    4

I am trying to select specific objects- primaily the icon url and text forecast from each period from this JSON file and not having much luck. I was hoping someone could point me in the right direction.

 

 

I am using php to decode the file and then assigning a variable to the object. My problem is none of the combinations I have tried seem to capture any data.

 

TIA

LAser

 

 

PHP file

$parsed_json = json_decode($json_string);

$icon0 = $parsed_json->{'forecast'}->{'icon'};
 
$icon_url0 = 
$parsed_json->{'txt_forecast'}->{'period[0]'}->{'icon_url'};

$title0 = 
$parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'period[0]'}->{'title'};

$fcttext0 = 
$parsed_json->{'forecastday'}->{'period[0]'}->{'fcttext'};
echo "Forecast: $fcttext0  <br />\n";

JSON file

{
"response": {
"version": "0.1"
,"features": {
"forecast": 1
}
}
,
"forecast":{
"txt_forecast": {
"date":"2:00 PM PDT",
"forecastday": [
{
"period":0,
"icon":"clear",
"icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif",
"title":"Friday",
"fcttext":"Clear. High of 73F. Breezy. Winds from the West at 10 to 20 
mph.",
"fcttext_metric":"Clear. High of 23C. Windy. Winds from the West at 15 to 30 
km/h.",
"pop":"0"
}
,
{
"period":1,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Friday Night",
"fcttext":"Partly cloudy. Low of 54F. Winds from the West at 5 to 15 
mph.",
"fcttext_metric":"Partly cloudy. Low of 12C. Breezy. Winds from the West at 
10 to 20 km/h.",
"pop":"0"
}
,
{
"period":2,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Saturday",
"fcttext":"Partly cloudy in the morning, then clear. High of 72F. Breezy. 
Winds from the West at 10 to 20 mph.",
"fcttext_metric":"Partly cloudy in the morning, then clear. High of 22C. 
Windy. Winds from the West at 15 to 30 km/h.",
"pop":"0"
}
,
{
"period":3,
 
and so on to period 7......

dump of the parsed json string

stdClass Object ( [response] => stdClass Object ( [version] => 0.1 
[termsofService] => http://www.wunderground.com/weather/api/d/terms.html 
[features] => stdClass Object ( [forecast] => 1 ) ) [forecast] => 
stdClass Object ( [txt_forecast] => stdClass Object ( [date] => 2:00 AM 
PDT [forecastday] => Array ( [0] => stdClass Object ( [period] => 0 
[icon] => clear [icon_url] => http://icons-ak.wxug.com/i/c/k/clear.gif 
[title] => Saturday [fcttext] => Clear. High of 73F. Breezy. Winds from 
the West at 10 to 20 mph. [fcttext_metric] => Clear. High of 23C. Windy. 
Winds from the West at 15 to 30 km/h. [pop] => 0 ) [1] => stdClass Object 
( [period] => 1 [icon] => partlycloudy [icon_url] => 
http://icons-ak.wxug.com/i/c/k/partlycloudy.gif [title] => Saturday Night 
[fcttext] => Mostly cloudy. Low of 55F. Breezy. Winds from the West at 10 to 
20 mph. [fcttext_metric] => Mostly cloudy. Low of 13C. Windy. Winds from the 
West at 20 to 30 km/h. [pop] => 20 ) [2] => stdClass Object ( [period] 
=> 2 [icon] => chancerain [icon_url] => 
http://icons-ak.wxug.com/i/c/k/chancerain.gif [title] => Sunday [fcttext] 
=> Partly cloudy with a chance of rain. High of 70F. Winds from the WSW at 10 
to 15 mph. Chance of rain 40%. [fcttext_metric] => Partly cloudy with a 
chance of rain. High of 21C. Breezy. Winds from the WSW at 15 to 25 km/h. Chance 
of rain 40%. [pop] => 40 ) [3] => stdClass Object ( [period] => 3 and 
on to period 7

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
CrispCreations    4

The way you're accessing the objects and properties is wrong, try this

$parsed_json = json_decode($json_string);
 
$icon0     = $parsed_json->forecast->txt_forecast->forecastday[0]->icon;

$icon_url0 = $parsed_json->forecast->txt_forecast->forecastday[0]->icon_url;

$title0    = $parsed_json->forecast->txt_forecast->forecastday[0]->title;

$fcttext0  = $parsed_json->forecast->txt_forecast->forecastday[0]->fcttext;

echo $icon0, '<br/>', $icon_url0, '<br/>', $title0, '<br/>', $fcttext0, '<br/>';
 
// result
clear
http://icons-ak.wxug.com/i/c/k/clear.gif
Friday
Clear. High of 73F. Breezy. Winds from the West at 10 to 20 mph.

Share this post


Link to post
Share on other sites
  • 0
Laser    4

CrispCreations

 

thanks you are correct that is what I was doing wrong. I got it all sorted. I appreciate the help

 

Laser

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.