2012-09-18 21 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