我正在尝试使用json和wunderground API请求一些数据。尝试检索数组时尝试获取非对象属性的错误
当我使用此代码时,它返回错误“消息:尝试获取非对象的属性”。
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/1e6a89f0a3aa092d/alerts/q/zmw:00000.1.16172.json");
$parsed_json = json_decode($json_string);
$wColor = $parsed_json->{'alerts'}->{'attribution'};
$wName = $parsed_json->{'alerts'}->{'wtype_meteoalarm'};
echo "Severe weather alert ${wColor} expected ${wName} - MORE INFO";
?>
数据是存在的,当我使用几乎相同的示例代码片段从文档
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/1e6a89f0a3aa092d/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
?>
它的工作原理可以在这里观看然而...
http://api.wunderground.com/api/1e6a89f0a3aa092d/alerts/q/zmw:00000.1.16172.json
绝对好!我第一次请求失败的原因是什么?
'alerts'是一个数组,并且没有'attribution'属性 – 2013-04-27 18:57:12