Easy-Json和PHP解码

时间:2014-04-26 23:34:38

标签: php json

我正在使用地下天气的API。我想在localhost上把一些值放到我的网站上。我没有问题包含很多价值观。像:

  

温度:30°F

     

风:快

以下是这些值的json:

enter image description here

"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不同的位置。

例如。我想回应当前的情况,就是这里:

enter image description here

问题在于:

{'forecast'}->{'simpleforecast'}->{'forecastday'}然后当天为零,第二天为1,第二天为2,第二天为下一天为3。

当我尝试在php中执行此操作时:

{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'0'}->{'conditions'};

它没有显示任何内容。如果在数组中有一个0?

,我怎么能进入值json

PD:对于0,有一个条件,1(即第二天)有其他条件,就像其他日子一样。感谢

2 个答案:

答案 0 :(得分:2)

对我来说它运行正常,我只是测试了你的代码,我只是修改了一下:

<?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>';

可以访问。

答案 1 :(得分:1)

你真的很接近但是{'0'}表示有一个带有0键的对象,而你真的想要访问forecastday的第一个索引

var_dump($parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[0]->conditions);