首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wunderground

Wunderground
EN

Stack Overflow用户
提问于 2015-11-10 20:05:28
回答 1查看 205关注 0票数 0

我只是在JSON上学到了一点,目前我正在尝试使用天气地下API和PHP来显示温度和天气状况,我已经显示了温度,但没有显示天气状态。这是我的代码:

代码语言:javascript
复制
<?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}";
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-10 20:08:27

您忘记了$parsed_json对象访问中的一个节点,替换:

代码语言:javascript
复制
$weather =$parsed_json->{'weather'};

有:

代码语言:javascript
复制
$weather = $parsed_json->current_observation->weather;

和更一般的用途:

代码语言:javascript
复制
$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}";
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33638582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档