2017-03-02 51 views
-1

看起來,從Wunderground讀取'條件'JSON輸出與從「警報」中準備輸出非常不同。以下是我的代碼的簡化版本。如何使用PHP從Wunderground讀取ALERT的JSON輸出?

$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. 

有人可以幫我寫代碼來使用'描述'和其他輸出嗎?

+0

警報是一個數組,所以你必須爲每個訪問說明使用循環 – mickadoo

回答

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); 
1
$alert_array = $parsed_jsonalert->alerts; 
foreach($alert_array as $alert) { 
    echo $alert->description; 
}