1

我正在尝试显示所有附近气象站的列表。我有代码:

$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");

$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
   $station = $stations[$i];
   if (count($station) > 1)
   {
    echo "City: " . $station->{'city'} . "\n";
    echo "State: " . $station->{'state'} . "\n";
    echo "Latitude: " . $station->{'lat'} . "\n";
    echo "Longitude: " . $station->{'lon'} . "\n";
   }
}

但目前它没有显示任何内容,我已经搜索了示例,但我找不到任何解决此问题的方法。

4

1 回答 1

0

或者,您可以使用 simpleforeach来迭代这些值。考虑这个例子:

$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
    echo "<hr/>";
    echo "City: " . $value['city'] . "<br/>";
    echo "State: " . $value['state'] . "<br/>";
    echo "Latitude: " . $value['lat'] . "<br/>";
    echo "Longitude: " . $value['lon'] . "<br/>";
    echo "<hr/>";
}

样品小提琴

于 2014-05-28T07:32:09.460 回答