如何使用PHP从Wunderground读取ALERTS的JSON输出?

时间:2017-03-02 22:08:07

标签: php json wunderground

从Wunderground读取'条件'JSON输出似乎与准备'alert'的输出非常不同。下面是我的代码的简化版本。

$json_stringalert = file_get_contents("http://api.wunderground.com/api/myKey/alerts/q/MO/Kansas_City.json");
$parsed_jsonalert = json_decode($json_stringalert);
$description = $parsed_jsonalert->{'alerts'}->{'description'}; 
echo "${description}

回声是空白的,没有错误。 URL的输出部分看起来像这样;

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "alerts": 1
  }
    }
        ,"query_zone": "037",
    "alerts": [
        {
        "type": "FIR",
        "description": "Fire Weather Warning",
        "date": "10:27 am CST on March 2, 2017",
        "date_epoch": "1488472020",
        "expires": "6:00 PM CST on March 02, 2017",
        "expires_epoch": "1488499200",
        "tz_short":"CST",
        "tz_long":"America/Chicago",
        "message": "\u000A...Red flag warning in effect until 6 PM CST this evening for\u000Abreezy northwest winds and low relative humidity values for fire \u000Aweather zones 020, 021, 025, 028, 029, 030, 037, 038, 043, 044, \u000A045, 053, 054, 057, 060, 102, 103, 104, and 105...\u000A\u000AThe National Weather Service in Kansas City/Pleasant Hill has\u000Aissued a red flag warning, which is in effect until 6 PM CST this\u000Aevening. \u000A\u000A* Affected area...fire weather zones 025, 057, 060, 102, 103, \u000A 104, and 105.Fire weather zones 020, 021, 028, 029, 030, 037, \u000A 038, 043, 044, 045, 053, and 054. \u000A\u000A* Wind...sustained northwest winds of 20 mph with higher gusts are\u000A expected this afternoon. \u000A\u000A* Humidity...relative humidity values will fall into the low to\u000A middle 20 percent range. \u000A\u000A* Impacts...any fires that develop will likely spread rapidly. \u000A Outdoor burning is not recommended.\u000A\u000APrecautionary/preparedness actions...\u000A\u000AA red flag warning means that critical fire weather conditions\u000Aare either occurring now, or will shortly. A combination of\u000Astrong winds, low relative humidity, and warm temperatures can\u000Acontribute to extreme fire behavior.\u000A\u000A\u000A\u000ACramer\u000A\u000A\u000A",
        "phenomena": "FW",
        "significance": "W", ...
and more below.

有人可以帮我写代码以使用'description'和其他输出吗?

2 个答案:

答案 0 :(得分:1)

$alert_array = $parsed_jsonalert->alerts; 
foreach($alert_array as $alert) {
    echo $alert->description;
}

答案 1 :(得分:0)

在您的JSON输出中,警报键是一个数组,因此$json->alerts->description无法访问任何内容。我假设你想要所有提醒而不只是一个特定的密钥($json->alerts[0]->description),所以你需要像这样循环数据:

function getAlerts($jsonString)
{
  $parsedObj = json_decode($jsonString);

  // This is the entire array of alert objects.
  $alertsArray = $parsedObj->alerts;

  // This next section is only necessary if you want to extract specific 
  // keys to be returned instead of the entire array of objects. If you
  // want everything than just return the $alertsArray variable.
  $alerts = array();

  foreach ($alertsArray as $alert) {
    // Add single key to alerts return array.
    // alternatively here is where you would build a filtered array
    // of values to return if you need more than one.
    $alerts[] = $alert->description;
  }
  return $alerts;
}
$file = file_get_contents("http://api.wunderground.com/api/myKey/alerts/q/MO/Kansas_City.json");
$alerts = getAlerts($file);
echo implode("\n", $alerts);