最近开始慢慢做毕业设计了,遇到一个功能是获取天气预报的,我选择的是和风天气的api,返回的是JSON数据,所以遇到了解析JSON的问题
首先简单说下JSON,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。用于数据转化传输,通用于PHP、Java、C++、C#、Python等编程语言的数据交换传输
在解析框架中,我选择了使用Gson,Gson是Google公司开发的一个开源框架,用于Java对象和JSON数据之间的相互转化。
Gson的下载地址:
https://github.com/google/gson
将包导入idea
Gson的使用:
//new一个Gson对象 Gson gson = new Gson(); //将json字符串转为bean对象 Hewind hewind = gson.fromJson(response,Hewind.class);
上面第四行代码中,调用了fromJson方法,第一个参数为json字符串,第二个参数是根据json建立的JavaBean
简单的实体类花花时间还是能完成的,但面对这样的数据就有点困难了
{"HeWeather6":[{"basic":{"cid":"CN101210301","location":"嘉兴","parent_city":"嘉兴","admin_area":"浙江","cnty":"中国","lat":"30.76265335","lon":"120.75086212","tz":"+8.0"},"update":{"loc":"2017-11-22 19:52","utc":"2017-11-22 11:52"},"status":"ok","now":{"cloud":"0","cond_code":"101","cond_txt":"多云","fl":"9","hum":"65","pcpn":"0","pres":"1023","tmp":"11","vis":"10","wind_deg":"339","wind_dir":"西北风","wind_sc":"微风","wind_spd":"7"},"daily_forecast":[{"cond_code_d":"305","cond_code_n":"100","cond_txt_d":"小雨","cond_txt_n":"晴","date":"2017-11-22","hum":"72","mr":"09:21","ms":"20:00","pcpn":"0.1","pop":"69","pres":"1024","sr":"06:29","ss":"16:58","tmp_max":"13","tmp_min":"5","uv_index":"4","vis":"9","wind_deg":"298","wind_dir":"西北风","wind_sc":"微风","wind_spd":"8"},{"cond_code_d":"100","cond_code_n":"100","cond_txt_d":"晴","cond_txt_n":"晴","date":"2017-11-23","hum":"48","mr":"10:07","ms":"20:50","pcpn":"0.0","pop":"0","pres":"1027","sr":"06:30","ss":"16:58","tmp_max":"12","tmp_min":"3","uv_index":"4","vis":"10","wind_deg":"271","wind_dir":"西风","wind_sc":"微风","wind_spd":"14"},{"cond_code_d":"100","cond_code_n":"101","cond_txt_d":"晴","cond_txt_n":"多云","date":"2017-11-24","hum":"44","mr":"10:51","ms":"21:42","pcpn":"0.0","pop":"0","pres":"1027","sr":"06:31","ss":"16:58","tmp_max":"13","tmp_min":"4","uv_index":"4","vis":"10","wind_deg":"177","wind_dir":"南风","wind_sc":"微风","wind_spd":"12"}],"hourly":[{"cloud":"55","cond_code":"101","cond_txt":"多云","dew":"5.4","hum":"78","pop":"0","pres":"1026","time":"2017-11-22 22:00","tmp":"8","wind_deg":"331","wind_dir":"西北风","wind_sc":"微风","wind_spd":"11"},{"cloud":"37","cond_code":"104","cond_txt":"阴","dew":"3.8","hum":"76","pop":"0","pres":"1027","time":"2017-11-23 01:00","tmp":"6","wind_deg":"341","wind_dir":"西北风","wind_sc":"微风","wind_spd":"11"},{"cloud":"16","cond_code":"103","cond_txt":"晴间多云","dew":"0.9","hum":"67","pop":"0","pres":"1027","time":"2017-11-23 04:00","tmp":"6","wind_deg":"338","wind_dir":"西北风","wind_sc":"微风","wind_spd":"11"},{"cloud":"3","cond_code":"103","cond_txt":"晴间多云","dew":"-1.3","hum":"60","pop":"0","pres":"1029","time":"2017-11-23 07:00","tmp":"6","wind_deg":"337","wind_dir":"西北风","wind_sc":"微风","wind_spd":"13"},{"cloud":"0","cond_code":"103","cond_txt":"晴间多云","dew":"-3.4","hum":"39","pop":"0","pres":"1029","time":"2017-11-23 10:00","tmp":"6","wind_deg":"338","wind_dir":"西北风","wind_sc":"微风","wind_spd":"15"},{"cloud":"0","cond_code":"103","cond_txt":"晴间多云","dew":"-7.7","hum":"26","pop":"0","pres":"1027","time":"2017-11-23 13:00","tmp":"10","wind_deg":"330","wind_dir":"西北风","wind_sc":"微风","wind_spd":"14"},{"cloud":"0","cond_code":"103","cond_txt":"晴间多云","dew":"-3.1","hum":"44","pop":"0","pres":"1026","time":"2017-11-23 16:00","tmp":"10","wind_deg":"308","wind_dir":"西北风","wind_sc":"微风","wind_spd":"9"},{"cloud":"1","cond_code":"103","cond_txt":"晴间多云","dew":"-1.4","hum":"63","pop":"0","pres":"1027","time":"2017-11-23 19:00","tmp":"7","wind_deg":"276","wind_dir":"西风","wind_sc":"微风","wind_spd":"6"}],"lifestyle":[{"brf":"较舒适","txt":"今天夜间虽然天气晴好,但会感觉偏凉,舒适、宜人。","type":"comf"},{"brf":"较冷","txt":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","type":"drsg"},{"brf":"较易发","txt":"天凉,昼夜温差较大,较易发生感冒,请适当增减衣服,体质较弱的朋友请注意适当防护。","type":"flu"},{"brf":"较适宜","txt":"天气较好,但考虑风力较强且气温较低,推荐您进行室内运动,若在户外运动注意防风并适当增减衣物。","type":"sport"},{"brf":"适宜","txt":"天气较好,风稍大,但温度适宜,是个好天气哦。适宜旅游,您可以尽情地享受大自然的无限风光。","type":"trav"},{"brf":"中等","txt":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。","type":"uv"},{"brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。","type":"cw"},{"brf":"良","txt":"气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。","type":"air"}]}]}
这个时候就用到了GsonFormat插件
安装方法:
安装后重启idea即可使用
使用方法:
使用快捷键 alt+s / alt+insert
把json数据粘贴进去,点击OK,JavaBean就自动生成啦
当然Gson的功能和使用方法不仅仅只有这些,我也只是初步接触,等我继续慢慢摸索