[英]Easy- Json and PHP decode
我正在使用地下天氣的API。 我想在localhost上把一些值放到我的網站上。 我沒有問題包含很多價值觀。 喜歡:
溫度:30°F
風:快
這是這些值的json:
"current_observation": {
"image": {
"url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
"display_location": {
"full":"Buenos Aires, Argentina",
"city":"Buenos Aires",
"state":"",
"state_name":"Argentina",
"country":"AG",
"country_iso3166":"AR",
"zip":"00000",
"magic":"1",
"wmo":"87582",
"latitude":"-34.56999969",
"longitude":"-58.41999817",
"elevation":"6.00000000"
},
"observation_location": {
"full":"Palermo, Buenos Aires, Ciudad Autónoma de Buenos Aires",
"city":"Palermo, Buenos Aires",
"state":"Ciudad Autónoma de Buenos Aires",
"country":"Argentina",
"country_iso3166":"AR",
"latitude":"-34.595318",
"longitude":"-58.419781",
"elevation":"124 ft"
},
"estimated": {
},
"station_id":"IBUENOSA157",
"observation_time":"Last Updated on April 26, 7:52 PM ART",
"observation_time_rfc822":"Sat, 26 Apr 2014 19:52:51 -0300",
"observation_epoch":"1398552771",
"local_time_rfc822":"Sat, 26 Apr 2014 19:52:52 -0300",
"local_epoch":"1398552772",
"local_tz_short":"ART",
"local_tz_long":"America/Buenos_Aires",
"local_tz_offset":"-0300",
"weather":"Clear",
"temperature_string":"65.8 F (18.8 C)",
"temp_f":65.8,
"temp_c":18.8,
"relative_humidity":"63%",
並在索引php文件中:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/f84c5a4cd54b3216/geolookup/alerts/astronomy/almanac/conditions/forecast/hourly/q/autoip.json");
$parsed_json = json_decode($json_string);
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
echo "{$'temp_c'};
這顯示了溫度。 json代碼中的溫度為: Current_observation
,然后是temp_c
值。
問題是我想回應預測,預測與temp_c
位於不同的位置。
例如。 我想回應當前的情況,就是這里:
問題是,那是:
{'forecast'}->{'simpleforecast'}->{'forecastday'}
然后當天為零,第二天為1,第二天為2,下一天為3到第二天。
當我嘗試在php中執行此操作時:
{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'0'}->{'conditions'};
它沒有顯示任何東西。 如果在數組中有0,我怎么能進入值json?
PD:對於0,有一個條件,1(即第二天)有其他條件,就像其他日子一樣。 謝謝
對我來說它只是工作正常,我只是測試了你的代碼,我只是稍微修改了一下:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/f84c5a4cd54b3216/geolookup/alerts/astronomy/almanac/conditions/forecast/hourly/q/autoip.json");
$parsed_json = json_decode($json_string, true);
$desired_forecast = $parsed_json['forecast']['simpleforecast']['forecastday'][0]['conditions'];
echo '<pre>';
print_r($desired_forecast); // Thunderstorm
echo '</pre>';
它是可訪問的。
你真的很接近但{'0'}
意味着有一個對象的密鑰為0
,而你真的想要訪問forecastday
的第一個索引
var_dump($parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[0]->conditions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.