JSON 响应上的未定义属性


Undefined property on JSON response

我正在尝试解析来自此链接的 JSON 对象 api.worldweather.com

这是我的php代码:

<?php
$base_url = "http://api.worldweatheronline.com/free/v2/weather.ashx?key=3be20163414371b55549cb84e2e47&q=" . "durgapur+wb&format=json";
$ret = file_get_contents($base_url);
$data = json_decode($ret);
$type = $data->data->current_condition->FeelsLikeC;
echo $type;
?>

任何帮助真的将不胜感激。谢谢。。。

current_condition 下,仍然有一个维度(数组):

stdClass Object
(
    [data] => stdClass Object
        (
            [current_condition] => Array
                ( // index 0, this is an array
                    [0] => stdClass Object
                        (
                            [cloudcover] => 34
                            [FeelsLikeC] => 28

所以你需要一个索引 0:

$base_url = "http://api.worldweatheronline.com/free/v2/weather.ashx?key=3be20163414371b55549cb84e2e47&q=" . "durgapur+wb&format=json";
$data = json_decode(file_get_contents($base_url));
$type = $data->data->current_condition[0]->FeelsLikeC;
                                    // ^ another nesting
echo $type;

示例输出