2012-09-18 34 views
0

我想分析的天氣API(URL)城市名稱,溫度等如何解析jQuery中

我的JSON數據下面的天氣API如下:

{ 
    "data": { 
     "current_condition": [{ 
      "cloudcover": "25", 
      "humidity": "70", 
      "observation_time": "04:21 PM", 
      "precipMM": "0.3", 
      "pressure": "1007", 
      "temp_C": "30", 
      "temp_F": "86", 
      "visibility": "4", 
      "weatherCode": "113", 
      "weatherDesc": [{ 
       "value": "Clear"}], 
      "weatherIconUrl": [{ 
       "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"}], 
      "winddir16Point": "S", 
      "winddirDegree": "180", 
      "windspeedKmph": "7", 
      "windspeedMiles": "4"}], 
     "request": [{ 
      "query": "Ahmedabad, India", 
      "type": "City"}], 
     "weather": [{ 
      "date": "2012-09-18", 
      "precipMM": "2.1", 
      "tempMaxC": "32", 
      "tempMaxF": "89", 
      "tempMinC": "25", 
      "tempMinF": "76", 
      "weatherCode": "176", 
      "weatherDesc": [{ 
       "value": "Patchy rain nearby"}], 
      "weatherIconUrl": [{ 
       "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}], 
      "winddir16Point": "SSW", 
      "winddirDegree": "203", 
      "winddirection": "SSW", 
      "windspeedKmph": "12", 
      "windspeedMiles": "8"}, 
     { 
      "date": "2012-09-19", 
      "precipMM": "3.4", 
      "tempMaxC": "32", 
      "tempMaxF": "89", 
      "tempMinC": "25", 
      "tempMinF": "76", 
      "weatherCode": "176", 
      "weatherDesc": [{ 
       "value": "Patchy rain nearby"}], 
      "weatherIconUrl": [{ 
       "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}], 
      "winddir16Point": "SW", 
      "winddirDegree": "223", 
      "winddirection": "SW", 
      "windspeedKmph": "12", 
      "windspeedMiles": "7"}] 
    } 
}​ 

我如何解析這些數據,並獲得城市名稱和溫度..我不知道..感謝在adavance。

===============輸出=======================

我想獲取數據是這樣,並設置文本框

Date  2012-09-18 2012-09-19 

tempMaxC 32    32 
tempMinC 25    25 

tempMaxF 89    89 
tempMinF 76    76 
+0

你嘗試過什麼?告訴我們你的想法。順便說一句:你如何獲得JSON,它是否已經解析爲一個JS對象? – Bergi

+0

如果您格式化數據,您將能夠弄清楚 - > http://jsfiddle.net/88fq6/ –

+0

我們希望您自己試圖解決這個問題,而不是要求社區達成完整的解決方案爲你。當你有一些代碼向我們展示,證明你有一些努力(即使它是錯誤的),請更新你的問題和標誌重新打開。謝謝。 – Kev

回答

3

如果檢索完這個JSON作爲一個字符串,那麼這個字符串傳遞給JSON.parse()*,然後訪問檢索到的值作爲常規的JavaScript對象:

var jsonStr = '{ "data": { "current_condition": [ {"cloudcover": "25", "humidity": "70", "observation_time": "04:21 PM", "precipMM": "0.3", "pressure": "1007", "temp_C": "30", "temp_F": "86", "visibility": "4", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "S", "winddirDegree": "180", "windspeedKmph": "7", "windspeedMiles": "4" } ], "request": [ {"query": "Ahmedabad, India", "type": "City" } ], "weather": [ {"date": "2012-09-18", "precipMM": "2.1", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SSW", "winddirDegree": "203", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "8" }, {"date": "2012-09-19", "precipMM": "3.4", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176", "weatherDesc": [ {"value": "Patchy rain nearby" } ], "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SW", "winddirDegree": "223", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "7" } ] }}', 
    jsonObj = JSON.parse(jsonStr); 
    console.log(jsonObj.data.current_condition[0].temp_F); 

Othe rwise,如果你已經檢索到這個JSON例如一些jQuery的$.ajax()成功回調的參數,它已經是一個對象,你並不需要調用JSON.parse(),而只是直接檢索對象的值:

$.getJSON("http://example.com/weather.json", function(jsonObj) { 
    // The response string is already parsed with $.parseJSON(), 
    // so you don't need to parse it yourself. 
    // Therefore just go ahead and access the properties of JavaScript object. 
    console.log(jsonObj.data.current_condition[0].temp_F); 
}); 

*如果你打算支持舊的瀏覽器(例如IE7)不支持JSON.parse/stringify,您需要包含JSON library

UPDATE:

DEMO針對特定的情況下

+0

我編輯我的問題並編寫輸出格式。我想要輸出like.please再次閱讀問題,並給我支持請 – ckpatel

+0

檢查我的演示(http://jsfiddle.net/f0t0n/Jv6Gk/)得到一個想法。 –

+0

非常感謝你花花公子 – ckpatel