无法从气象站解析json数组

时间:2019-04-03 19:46:53

标签: php arrays json

我得到一些json数据,如下所示:

select VALUE sum(c.total) AS TOTALOWED
FROM (select VALUE [c.title, c.total]
FROM c

这使我得到以下信息:

$data = json_decode(file_get_contents("$api/$features$key$query"));
print_r2($data);

当我尝试获取位置信息时,我会使用:

stdClass Object
(
    [location] => stdClass Object
        (
            [name] => Boston, Logan International Airport
            [region] => 
            [country] => United States
            [lat] => 42.36
            [lon] => -71.01
            [tz_id] => America/New_York
            [localtime_epoch] => 1554320436
            [localtime] => 2019-04-03 15:40
        )

    [current] => stdClass Object
        (
            [condition] => stdClass Object
                (
                )

            [uv] => 5
        )

    [forecast] => stdClass Object
        (
            [forecastday] => Array
                (
                    [0] => stdClass Object
                        (
                            [day] => stdClass Object
                                (
                                    [condition] => stdClass Object
                                        (
                                        )

                                )

                            [astro] => stdClass Object
                                (
                                )

                            [hour] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [time] => 2019-04-03 00:00
                                            [temp_f] => 41
                                            [condition] => stdClass Object
                                                (
                                                    [text] => Moderate rain
                                                )

                                            [wind_mph] => 9.4
                                            [wind_degree] => 91
                                            [wind_dir] => E
                                            [pressure_in] => 30.7
                                            [precip_mm] => 1
                                            [humidity] => 75
                                            [cloud] => 94
                                            [feelslike_f] => 35.1
                                            [windchill_f] => 35.1
                                            [heatindex_f] => 41
                                            [dewpoint_f] => 33.4
                                            [will_it_rain] => 0

但这无法获取数据,我也尝试过= $ data ['location'] ['name']但仍然没有数据

1 个答案:

答案 0 :(得分:2)

使用第二个参数true

$data = json_decode(file_get_contents("$api/$features$key$query"),true);

这会将您的数据转换为关联数组,然后您就可以像关联数组一样对其进行操作。

转换它的方式是一个对象,这就是为什么您必须将其视为对象。
不用打电话

$location = $data['location'];

您必须拥有

$location = $data->location;