Jump to content

Beginner needing help with json script


mkozlow

Recommended Posts

So im trying to use a script to fetch the weather forecast from weatherunderground and im using their weather API functionality.

 

Basically im trying to get the output to be something like (Continuing down in a list)

(Day of week): (Current conditions) (High temp)-(Low temp)

 

this is what I have:

#!/usr/bin/php
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/MY_APIKEY/forecast/q/19104.json");
$parsed_json = json_decode($json_string);
$day = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'date'}->{'weekday_short'};
$high = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'high'}->{'fahrenheit'};
$low = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'low'}->{'fahrenheit'};
$conditions = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'conditions'};

echo "Forecast:\n";
echo "${day}: ${conditions} ${high}-${low}\n";
?>

 

the output of the get_file_contents is:

 "response":  {
    "version": "0.1",
    "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
    "features":  {
      "forecast": 1
    }
  },
  "forecast":  {
    "txt_forecast":  {},
    "simpleforecast":  {
      "forecastday":  [
         {
          "date":  {
            "epoch": "1330398000",
            "pretty": "10:00 PM EST on February 27, 2012",
            "day": 27,
            "month": 2,
            "year": 2012,
            "yday": 57,
            "hour": 22,
            "min": "00",
            "sec": 0,
            "isdst": "0",
            "monthname": "February",
            "weekday_short": "Mon",
            "weekday": "Monday",
            "ampm": "PM",
            "tz_short": "EST",
            "tz_long": "America/New_York"
          },
          "period": 1,
          "high":  {
            "fahrenheit": "59",
            "celsius": "15"
          },
          "low":  {
            "fahrenheit": "36",
            "celsius": "2"
          },
          "conditions": "Partly Cloudy",
          "icon": "partlycloudy",
          "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
          "skyicon": "mostlysunny",
          "pop": 0,
          "qpf_allday":  {
            "in": 0,
            "mm": 0
          },
          "qpf_day":  {
            "in": 0,
            "mm": 0
          },
          "qpf_night":  {
            "in": 0,
            "mm": 0
          },
          "snow_allday":  {
            "in": 0,
            "cm": 0
          },
          "snow_day":  {
            "in": 0,
            "cm": 0
          },
          "snow_night":  {
            "in": 0,
            "cm": 0
          },
          "maxwind":  {
            "mph": 9,
            "kph": 14,
            "dir": "South",
            "degrees": 180
          },
          "avewind":  {
            "mph": 6,
            "kph": 10,
            "dir": "South",
            "degrees": 171
          },
          "avehumidity": 65,
          "maxhumidity": 78,
          "minhumidity": 48
        },
         {
          "date":  {
            "epoch": "1330484400",
            "pretty": "10:00 PM EST on February 28, 2012",
            "day": 28,
            "month": 2,
            "year": 2012,
            "yday": 58,
            "hour": 22,
            "min": "00",
            "sec": 0,
            "isdst": "0",
            "monthname": "February",
            "weekday_short": "Tue",
            "weekday": "Tuesday",
            "ampm": "PM",
            "tz_short": "EST",
            "tz_long": "America/New_York"
          },
          "period": 2,
          "high":  {
            "fahrenheit": "50",
            "celsius": "10"
          },
          "low":  {
            "fahrenheit": "37",
            "celsius": "3"
          },
          "conditions": "Clear",
          "icon": "clear",
          "icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
          "skyicon": "mostlysunny",
          "pop": 0,
          "qpf_allday":  {
            "in": 0,
            "mm": 0
          },
          "qpf_day":  {
            "in": 0,
            "mm": 0
          },
          "qpf_night":  {
            "in": 0,
            "mm": 0
          },
          "snow_allday":  {
            "in": 0,
            "cm": 0
          },
          "snow_day":  {
            "in": 0,
            "cm": 0
          },
          "snow_night":  {
            "in": 0,
            "cm": 0
          },
          "maxwind":  {
            "mph": 8,
            "kph": 13,
            "dir": "NW",
            "degrees": 318
          },
          "avewind":  {
            "mph": 5,
            "kph": 8,
            "dir": "ESE",
            "degrees": 123
          },
          "avehumidity": 53,
          "maxhumidity": 69,
          "minhumidity": 39
        },
         {
          "date":  {
            "epoch": "1330570800",
            "pretty": "10:00 PM EST on February 29, 2012",
            "day": 29,
            "month": 2,
            "year": 2012,
            "yday": 59,
            "hour": 22,
            "min": "00",
            "sec": 0,
            "isdst": "0",
            "monthname": "February",
            "weekday_short": "Wed",
            "weekday": "Wednesday",
            "ampm": "PM",
            "tz_short": "EST",
            "tz_long": "America/New_York"
          },
          "period": 3,
          "high":  {
            "fahrenheit": "43",
            "celsius": "6"
          },
          "low":  {
            "fahrenheit": "41",
            "celsius": "5"
          },
          "conditions": "Rain",
          "icon": "rain",
          "icon_url": "http://icons-ak.wxug.com/i/c/k/rain.gif",
          "skyicon": "cloudy",
          "pop": 80,
          "qpf_allday":  {
            "in": 0.71,
            "mm": 18
          },
          "qpf_day":  {
            "in": 0.5,
            "mm": 12.7
          },
          "qpf_night":  {
            "in": 0.21,
            "mm": 5.3
          },
          "snow_allday":  {
            "in": 0,
            "cm": 0
          },
          "snow_day":  {
            "in": 0,
            "cm": 0
          },
          "snow_night":  {
            "in": 0,
            "cm": 0
          },
          "maxwind":  {
            "mph": 16,
            "kph": 26,
            "dir": "ESE",
            "degrees": 106
          },
          "avewind":  {
            "mph": 9,
            "kph": 14,
            "dir": "ESE",
            "degrees": 115
          },
          "avehumidity": 91,
          "maxhumidity": 95,
          "minhumidity": 64
        },
         {
          "date":  {
            "epoch": "1330657200",
            "pretty": "10:00 PM EST on March 01, 2012",
            "day": 1,
            "month": 3,
            "year": 2012,
            "yday": 60,
            "hour": 22,
            "min": "00",
            "sec": 0,
            "isdst": "0",
            "monthname": "March",
            "weekday_short": "Thu",
            "weekday": "Thursday",
            "ampm": "PM",
            "tz_short": "EST",
            "tz_long": "America/New_York"
          },
          "period": 4,
          "high":  {
            "fahrenheit": "54",
            "celsius": "12"
          },
          "low":  {
            "fahrenheit": "32",
            "celsius": "0"
          },
          "conditions": "Overcast",
          "icon": "cloudy",
          "icon_url": "http://icons-ak.wxug.com/i/c/k/cloudy.gif",
          "skyicon": "cloudy",
          "pop": 0,
          "qpf_allday":  {
            "in": 0,
            "mm": 0
          },
          "qpf_day":  {
            "in": 0,
            "mm": 0
          },
          "qpf_night":  {
            "in": 0,
            "mm": 0
          },
          "snow_allday":  {
            "in": 0,
            "cm": 0
          },
          "snow_day":  {
            "in": 0,
            "cm": 0
          },
          "snow_night":  {
            "in": 0,
            "cm": 0
          },
          "maxwind":  {
            "mph": 10,
            "kph": 16,
            "dir": "WSW",
            "degrees": 253
          },
          "avewind":  {
            "mph": 4,
            "kph": 6,
            "dir": "West",
            "degrees": 280
          },
          "avehumidity": 95,
          "maxhumidity": 100,
          "minhumidity": 87
        }
      ]
    }
  }
}

 

I feel like i need to define different variables for each day like dayx, highx, lowx, conditionsx, etc where x=period and it increases the period every time you run through. I remember learning this in my intro to java class but in all honesty i forgot, even if i did know if i was on the right path.

 

Any help? I'd appreciate it.

 

Thanks

-Koz

Link to comment
Share on other sites

forecastday is actually an array of objects. You can (and should) foreach over it.

 

ok so like i said im a beginner and really dont know wtf im doing.

would i incorporate a foreach while parsing the output? or instead of?

 

would it be appropriate to do something like this (i feel like im missing some syntax (meaning brackets and ; and stuff if you notice shout out please. i should probably be using a script editor instead of notepad but meh):

foreach (forecastday as &$key) {
if ($key = $date) {
	foreach (date as &$d) {
		if $d = $weekday_short {
			$day = $d;}
		else{}
	}}
else if ($key = $high) {
	foreach (high as &$h) {
		if $h = $fahrenheit {
			$hi = $h;}
		else{}
	}}
else if ($key = $low) {
	foreach (low as &$l) {
		if $l = $fahrenheit {
			$lo = $l;}
		else{}
	}}
else if ($key = $conditions) {
	$cond = $key;}
}

Link to comment
Share on other sites

You're heading in the right direction but are traversing the data incorrectly.

 

<?php
$json = file_get_contents('weather.json');
$json = json_decode($json);

// You can learn a lot about how PHP interpreted the data by un-commenting these lines:
// print_r($json);
// exit;

// Now it's just a case of following the nested structure to the "forecastday" array.
// (which is an array of objects as requinix mentioned)
foreach ($json->forecast->simpleforecast->forecastday as $day)
{
echo "{$day->date->weekday}: {$day->conditions} <br>";
echo "{$day->low->fahrenheit} - {$day->high->fahrenheit} °F <br>";	
echo '<br>';
}
exit;
?>

 

Hope that helps clear it up.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.