简体   繁体   English

Easy-Json和PHP解码

[英]Easy- Json and PHP decode

I'm working with an API from underground weather. 我正在使用地下天气的API。 I'm trying to put some values into my website on localhost. 我想在localhost上把一些值放到我的网站上。 I include a lot of values without problem. 我没有问题包含很多价值观。 Like: 喜欢:

Temperature: 30°F 温度:30°F

Wind: Fast 风:快

Here is the json of those values: 这是这些值的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%",

and in the index php file: 并在索引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'};

That displays the temperature. 这显示了温度。 The temperature in the json code is in: Current_observation and then the value temp_c . json代码中的温度为: Current_observation ,然后是temp_c值。

The problem is that I want to echo the forecast, and the forecast is in a different location than the temp_c . 问题是我想回应预测,预测与temp_c位于不同的位置。

For example. 例如。 I want to echo the current conditions, that is here: 我想回应当前的情况,就是这里:

在此输入图像描述

The problem is, that is in: 问题是,那是:

{'forecast'}->{'simpleforecast'}->{'forecastday'} and then there is a zero for the current day, a 1 for the next day, a 2 for the following day, and a 3 for the next to the next day. {'forecast'}->{'simpleforecast'}->{'forecastday'}然后当天为零,第二天为1,第二天为2,下一天为3到第二天。

When I try to do that in php: 当我尝试在php中执行此操作时:

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

It does not show anything. 它没有显示任何东西。 How I can go into a value json when in the array there is a 0? 如果在数组中有0,我怎么能进入值json?

PD: for 0, there is one condition, for 1 (that is next day) there is other condition, and like that the others day. PD:对于0,有一个条件,1(即第二天)有其他条件,就像其他日子一样。 Thanks 谢谢

For me it just works fine, I just tested your code, I just modified it a little bit: 对我来说它只是工作正常,我只是测试了你的代码,我只是稍微修改了一下:

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

It is accessible. 它是可访问的。

你真的很接近但{'0'}意味着有一个对象的密钥为0 ,而你真的想要访问forecastday的第一个索引

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM