YQL查询google天气,js替换JSON结果为中文

编程技术  /  houtizong 发布于 3年前   91
其实这个真挺无聊的,因为天气查询通过了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});//天气图片});       })

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客