日期:2014-05-16  浏览次数:20378 次

YQL查询google天气,js替换JSON结果为中文
其实这个真挺无聊的,因为天气查询通过了YQL的接口,所以google返回是英文结果,就用js替换返回JSON里的值。
var w_city_name='nanning';
		//$("#w_click").click(function(){
		var w_url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fwww.google.com%2Fig%2Fapi%3Fweather%3D'+w_city_name+'%22&format=json&callback=?';
		$.getJSON(w_url,function(w_data){
			var temp_f = w_data.query.results.xml_api_reply.weather.current_conditions.temp_f.data;
			var temp_c = w_data.query.results.xml_api_reply.weather.current_conditions.temp_c.data;
			var humidity = w_data.query.results.xml_api_reply.weather.current_conditions.humidity.data.replace("Humidity:","");
			var icon = 'http://www.google.com/'+w_data.query.results.xml_api_reply.weather.current_conditions.icon.data;
			var condition=w_data.query.results.xml_api_reply.weather.current_conditions.condition.data;
			var wind_condition=w_data.query.results.xml_api_reply.weather.current_conditions.wind_condition.data.replace("Wind:","");
			//var condition=w_data.query.results.xml_api_reply.weather.forecast_conditions[0].condition.data;
			wind_condition = wind_condition.replace(/\b(NE|NW|NS|ES|EN|E|S|N|W|at|Wind:|mph)\b/g, function($0, $1) {
					    return {
							 "NE": "北风转东风"
							 ,"NS": "北风转南风"
							 ,"NW": "北风转东风"
							 ,"EN": "东风转北风"
					         ,"ES": "东风转南风"
							 ,"EW": "东风转南风"
							 ,"WN": "北风转东风"
							 ,"S":"南风"
							 ,"W":"西风"
							 ,"NW":"北风转西风"
							 ,"NS":"北风"
							 ,"N":"北风"
							  ,"E":"东风"
					         ,"Wind:": ""
					         , "at": "风速"
							 ,"mph":"米/每秒"
					    }[$1];
					});
			
			condition = condition.replace(/\b(Cloudy|Chance of Storm|Overcast|Clear|Chance of Rain|Mostly|Haze|Rain)\b/g, function($0, $1) {
					    return {
							
					        "Cloudy": "多云"
							,"Rain": "小雨"
					        ,"Chance of Storm": "大雨"
					        , "Overcast": "阴天"
							,"Clear":"晴朗"
							,"Chance of Rain":"小雨"
							,"Mostly":"",
							"Haze":"薄雾"
								
					    }[$1];
					});
				//alert(wind_condition);
				//$("#w_sd").append(humidity);//湿度
				$("#w_c").empty().append(temp_c);//摄氏度
				//$("#w_f").append(temp_f);//华氏度
				$("#w_info").append(condition);//天气信息
				//$("#w_wind").append(wind_condition);//风向
				//$("#w_icon").attr({ src: icon, alt:condition});//天气图片
			});	
			
			
       })