Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
轻松-Json和PHP解码_Php_Json - Fatal编程技术网

轻松-Json和PHP解码

轻松-Json和PHP解码,php,json,Php,Json,我正在使用《地下天气》的API。我正试图在本地主机上为我的网站添加一些值。我包括了很多没有问题的价值观。比如: 温度:30华氏度 风:快 以下是这些值的json: "current_observation": { "image": { "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png", "

我正在使用《地下天气》的API。我正试图在本地主机上为我的网站添加一些值。我包括了很多没有问题的价值观。比如:

温度:30华氏度

风:快

以下是这些值的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'};
它没有显示任何东西。当数组中存在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
,而您确实想访问
预测日的第一个索引


好的,您如何存储这些预测数据?它在变量中了吗?如果没有代码,“它什么也不显示”就没有多大帮助。问题是,我无法获取数据。例如,我会:$forecastcondition=$parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'0'}->{'conditions'};从您刚才在注释中输入的代码来看,我觉得您使用的是同一个变量,我忍不住认为您试图访问相同的JSON数据。事实上,如果它与您在上述问题中发布的内容相同,则没有可访问的“simpleforecast”对象。向我们展示您如何访问新的JSON信息。不是acutal JSON数据,而是如何在PHP脚本中访问它?我正要回答完全相同的问题!也就是说,将
true
的第二个参数添加到
json_decode()
中,这样就可以得到一个关联数组,而不是对象。然后,只需使用简单的方括号即可获得所需内容(包括
[0]
,无需引号即可获得数组中的第一项)。谢谢!工作正常,问题是Y忘记删除0上的“谢谢!这就是我做的坏事
<?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>';
var_dump($parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[0]->conditions);