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

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: 最近开始慢慢做毕业设计了,遇到一个功能是获取天气预报的,我选择的是和风天气的api,返回的是JSON数据,所以遇到了解析JSON的问题 首先简单说下JSON,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。

最近开始慢慢做毕业设计了,遇到一个功能是获取天气预报的,我选择的是和风天气的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的功能和使用方法不仅仅只有这些,我也只是初步接触,等我继续慢慢摸索

相关文章
|
2月前
|
Java
轻松上手Java字节码编辑:IDEA插件VisualClassBytes全方位解析
本插件VisualClassBytes可修改class字节码,包括class信息、字段信息、内部类,常量池和方法等。
146 6
|
3月前
|
SQL 存储 JSON
SQL,解析 json
SQL,解析 json
89 8
|
9天前
|
JSON JavaScript 前端开发
一次采集JSON解析错误的修复
两段采集来的JSON格式数据存在格式问题,直接使用PHP的`json_decode`会报错。解决思路包括:1) 手动格式化并逐行排查错误;2) 使用PHP-V8JS扩展在JavaScript环境中解析。具体方案一是通过正则表达式和字符串替换修复格式,方案二是利用V8Js引擎执行JS代码并返回JSON字符串,最终实现正确解析。 简介: 两段采集的JSON数据因掺杂JavaScript代码导致PHP解析失败。解决方案包括手动格式化修复和使用PHP-V8JS扩展在JavaScript环境中解析,确保JSON数据能被正确处理。
|
2月前
|
SQL 数据可视化 数据库
多维度解析低代码:从技术架构到插件生态
本文深入解析低代码平台,涵盖技术架构、插件生态及应用价值。通过图形化界面和模块化设计,低代码平台降低开发门槛,提升效率,支持企业快速响应市场变化。重点分析开源低代码平台的优势,如透明架构、兼容性与扩展性、可定制化开发等,探讨其在数据处理、功能模块、插件生态等方面的技术特点,以及未来发展趋势。
|
2月前
|
SQL 数据可视化 数据库
多维度解析低代码:从技术架构到插件生态
本文深入解析低代码平台,从技术架构到插件生态,探讨其在企业数字化转型中的作用。低代码平台通过图形化界面和模块化设计降低开发门槛,加速应用开发与部署,提高市场响应速度。文章重点分析开源低代码平台的优势,如透明架构、兼容性与扩展性、可定制化开发等,并详细介绍了核心技术架构、数据处理与功能模块、插件生态及数据可视化等方面,展示了低代码平台如何支持企业在数字化转型中实现更高灵活性和创新。
54 1
|
3月前
|
JSON JavaScript Java
在Java中处理JSON数据:Jackson与Gson库比较
本文介绍了JSON数据交换格式及其在Java中的应用,重点探讨了两个强大的JSON处理库——Jackson和Gson。文章详细讲解了Jackson库的核心功能,包括数据绑定、流式API和树模型,并通过示例演示了如何使用Jackson进行JSON解析和生成。最后,作者分享了一些实用的代码片段和使用技巧,帮助读者更好地理解和应用这些工具。
215 0
在Java中处理JSON数据:Jackson与Gson库比较
|
4月前
|
JSON API 数据格式
requests库中json参数与data参数使用方法的深入解析
选择 `data`或 `json`取决于你的具体需求,以及服务器端期望接收的数据格式。
323 2
|
3月前
|
JSON JavaScript API
商品详情数据接口解析返回的JSON数据(API接口整套流程)
商品详情数据接口解析返回的JSON数据是API接口使用中的一个重要环节,它涉及从发送请求到接收并处理响应的整个流程。以下是一个完整的API接口使用流程,包括如何解析返回的JSON数据:
|
JSON Java 数据格式

热门文章

最新文章

推荐镜像

更多