json解析—Gson以及GsonFormat插件的運用

最近開始慢慢作畢業設計了,遇到一個功能是獲取天氣預報的,我選擇的是和風天氣的api,返回的是JSON數據,因此遇到了解析JSON的問題git

 

首先簡單說下JSON,JSON(JavaScript Object Notation)是一種輕量級的數據交換格式。用於數據轉化傳輸,通用於PHP、Java、C++、C#、Python等編程語言的數據交換傳輸github

 

在解析框架中,我選擇了使用Gson,Gson是Google公司開發的一個開源框架,用於Java對象和JSON數據之間的相互轉化。編程

 

Gson的下載地址:json

https://github.com/google/gsonapi

 

將包導入idea框架

 

 

Gson的使用:編程語言

//new一個Gson對象
Gson gson = new Gson();
//將json字符串轉爲bean對象
Hewind hewind = gson.fromJson(response,Hewind.class);

上面第四行代碼中,調用了fromJson方法,第一個參數爲json字符串,第二個參數是根據json創建的JavaBeanide

 

簡單的實體類花花時間仍是能完成的,但面對這樣的數據就有點困難了google

{"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高於1五、PA+的防曬護膚品,戴帽子、太陽鏡。","type":"uv"},{"brf":"較適宜","txt":"較適宜洗車,將來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。","type":"cw"},{"brf":"良","txt":"氣象條件有利於空氣污染物稀釋、擴散和清除,可在室外正常活動。","type":"air"}]}]}

 

這個時候就用到了GsonFormat插件url

 

安裝方法:

 

安裝後重啓idea便可使用

 

使用方法:

使用快捷鍵 alt+s / alt+insert

 

把json數據粘貼進去,點擊OK,JavaBean就自動生成啦

 

 固然Gson的功能和使用方法不單單隻有這些,我也只是初步接觸,等我繼續慢慢摸索

相關文章
相關標籤/搜索