Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 神探_Php_Json - Fatal编程技术网

Php 神探

Php 神探,php,json,Php,Json,我刚刚学习了一点JSON,我正在尝试使用weather underground 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_strin

我刚刚学习了一点JSON,我正在尝试使用weather underground 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}";
?>

您在
$parsed\u 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}";

var\u dump($parsed\u json)
,然后查看您收到了什么。是的,您可以提示用户输入您想要的任何信息,然后可以根据需要使用这些信息。顺便说一下,所有这些大括号都是不必要的。对象属性通常会被引用
$like->this
,变量可以包含在
“双引号字符串”中
,没有任何问题。谢谢你,这很有效!谢谢,欢迎光临!别忘了将这个问题标记为已解决:)为什么OP要“尝试这个”?一个好的答案总会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了SO的未来访客。你说得对:)现在编辑了!