我只是在JSON上学到了一点,目前我正在尝试使用天气地下API和PHP来显示温度和天气状况,我已经显示了温度,但没有显示天气状态。这是我的代码:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/9fca46f2c0517556/geolookup/conditions/q/UK/Leeds.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$weather =$parsed_json->{'weather'};
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
echo "Current temperature in ${location} is: ${temp_c}\n degrees and it is currently ${weather}";
?>发布于 2015-11-10 20:08:27
您忘记了$parsed_json对象访问中的一个节点,替换:
$weather =$parsed_json->{'weather'};有:
$weather = $parsed_json->current_observation->weather;和更一般的用途:
$json_string = file_get_contents("http://api.wunderground.com/api/9fca46f2c0517556/geolookup/conditions/q/UK/Leeds.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->location->city;
$weather =$parsed_json->current_observation->weather;
$temp_c = $parsed_json->current_observation->temp_c;
echo "Current temperature in ${location} is: ${temp_c}\n degrees and it is currently ${weather}";https://stackoverflow.com/questions/33638582
复制相似问题