JSON响应的未定义属性

时间:2015-01-02 07:03:00

标签: php arrays json

我正在尝试解析来自此链接{J <3}}

的JSON对象

这是我的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;

?>

真的很感激任何帮助。感谢...

1 个答案:

答案 0 :(得分:4)

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;

Sample Output