PHP获取JSON详细信息

时间:2019-08-19 20:44:04

标签: php json

我有一个JSON文件(如下所示),其中包含我正在用php提取的天气信息,由于无法获取天气描述,我被卡住了。

  object(stdClass)#1 (7) {
  ["city_id"]=> int(2650225)
  ["main"]=> object(stdClass)#2 (5) {
        ["temp"]=> float(286.96)
        ["temp_min"]=> float(286.15)
        ["temp_max"]=> float(289.82)
        ["pressure"]=> int(1001)
        ["humidity"]=> int(66)
  }
  ["wind"]=> object(stdClass)#3 (2) {
        ["speed"]=> int(7)
        ["deg"]=> int(230)
  }
  ["clouds"]=> object(stdClass)#4 (1) {
    ["all"]=> int(40)
  }
  ["weather"]=> array(1) {
        [0]=> object(stdClass)#5 (4) {
            ["id"]=> int(802)
            ["main"]=> string(6) "Clouds"
            ["description"]=> string(16) "scattered clouds"
            ["icon"]=> string(3) "03d"
        }
  }
  ["dt"]=> int(1349096400)
  ["dt_iso"]=> string(29) "2012-10-01 13:00:00 +0000 UTC"
}

虽然我能够获得所有其他详细信息,例如城市和温度,风速等 我无法获取天气->主要或天气说明

任何帮助将不胜感激。

这是我为获取其他一些详细信息而做的事情:

<?php
$strJsonFileContents = file_get_contents($argv[1]);
$array = json_decode($strJsonFileContents);
$value = array_values($array)[0];

$city = $value->city_id;
$temp = (($value->main->temp) - 273.15);
$humidity = $value->main->humidity;
// FAILS
$cast = $value->weather->main;
$weather = $value->weather->description;
?>

1 个答案:

答案 0 :(得分:2)

属性wheater是一个数组。这就是为什么您无法直接访问maindescription的原因。试试这个:

$cast = $value->weather[0]->main;
$weather = $value->weather[0]->description;