2011-06-29 38 views
0

我只需要一點點幫助JSON和PHP。我怎麼回波數據certian部件,如果我的請求回來這樣看:幫助一些JSON數據和PHP

{ "data": { "current_condition": [ {"cloudcover": "2", "humidity": "54", "observation_time": "09:05 PM", "precipMM": "0.0", "pressure": "1019", "temp_C": "11", "visibility": "10", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "N", "winddirDegree": "350", "windspeedKmph": "15", "windspeedMiles": "9" } ], "request": [ {"query": "48.85,2.35", "type": "LatLon" } ] }} 

我使用的是天氣API,我的代碼目前的樣子:

$weather_url = file_get_contents("http://free.worldweatheronline.com/feed/weather.ashx?q=xxxxx&format=json&num_of_days=2&key=xxxxxxxxxxxxxxxxxx"); 
$json_output_w = json_decode($weather_url, true); 

將q的URL字符串可以是一個郵政編碼,緯度和長度,或城市,我知道它正在返回數據,因爲我可以轉儲變量$ json_output_w;但我只需要一點指導就可以實際迴應返回的數據的某些部分。就像我想回聲 windspeedMiles

+1

'的var_dump($ json_output_w);'? – zerkms

+0

我不想顯示所有的數據,我只是想要顯示某些信息,如echo $ json_output_w ['data'] ['current_condition'] ['cloudcover']中的某些信息;但這對我沒有任何幫助。 – mcbeav

+0

如果它沒有返回任何東西 - 那麼或者這個東西沒有任何東西,或者你試圖檢索一個不存在的關鍵字。所以你仍然需要'var_dump()'來檢查你的數組。 – zerkms

回答

3

json_decode()函數將返回一個對象或數組(取決於第二個參數)。您可以使用var_dump()功能探索退回的項目結構:

var_dump($json_output_w); 

從這裏,你會發現你需要拉出來的值時要考慮什麼類型的結構。爲了得到windSpeedMiles值,你會做以下幾點:

echo $json_output_w["data"]["current_condition"][0]["windspeedMiles"]; 

在線演示:http://codepad.org/BfhHbQMz

+1

用於鍵盤鏈接 –

+1

+1涼豆感謝! – mcbeav