0

I'm trying to parse some data from wunderground JSON, without using foreach cycle, only for the first day. Example from this JSON: http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json

I want to get only the first day:

$json_string = file_get_contents('http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json');
$parsed_json = json_decode($json_string);
$weekday = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday[0]'}->{'date'}->weekday;

I've googled a lot and tried lot's of examples, but usually i get error or nothing. help?

3
  • 3
    $parsed_json->forecast->simpleforecast->forecastday[0]->date->weekday? Commented Jan 14, 2013 at 20:17
  • Aah ok got it work. What's the difference? Commented Jan 14, 2013 at 20:20
  • forecastday is an array. When you were putting {'forecastday[0]'}, that's as if the name was forecastday[0]. The name is actually forecastday, and the array index is 0. Commented Jan 14, 2013 at 20:30

2 Answers 2

1

You were on the right track. It is just that you needed to evaluate the array element after getting the object in the forecastday

    <?php
    $json_string = file_get_contents('http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json');
    $parsed_json = json_decode($json_string);
    echo $weekday = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[0]->{'date'}->weekday;
    ?>

http://phpfiddle.org/main/code/7ws-pry

Sign up to request clarification or add additional context in comments.

Comments

0

You access the methods on the objects wrong:

Since I am not sure what property you want to get, here is how you determine what is in the variable:

 var_dump($parsed_json->forecast);

(And there is no "simpleforcast" there).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.