Java中利用gson解析Json实例教程
时间:2019-03-30
本文章向大家介绍Java中利用gson解析Json实例教程,主要包括Java中利用gson解析Json实例教程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
前言
本文主要跟大家介绍了关于Java用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。
json数据
{ "resultcode": "200", "reason": "successed!", "result": { "sk": { "temp": "24", "wind_direction": "西南风", "wind_strength": "2级", "humidity": "51%", "time": "10:11" }, "today": { "temperature": "16℃~27℃", "weather": "阴转多云", "weather_id": { "fa": "02", "fb": "01" }, "wind": "西南风3-4 级", "week": "星期四", "city": "滨州", "date_y": "2015年06月04日", "dressing_index": "舒适", "dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。", "uv_index": "最弱", "comfort_index": "", "wash_index": "较适宜", "travel_index": "", "exercise_index": "较适宜", "drying_index": "" }, "future": [ { "temperature": "16℃~27℃", "weather": "阴转多云", "weather_id": { "fa": "02", "fb": "01" }, "wind": "西南风3-4 级", "week": "星期四", "date": "20150604" }, { "temperature": "20℃~32℃", "weather": "多云转晴", "weather_id": { "fa": "01", "fb": "00" }, "wind": "西风3-4 级", "week": "星期五", "date": "20150605" }, { "temperature": "23℃~35℃", "weather": "多云转阴", "weather_id": { "fa": "01", "fb": "02" }, "wind": "西南风3-4 级", "week": "星期六", "date": "20150606" }, { "temperature": "20℃~33℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "北风微风", "week": "星期日", "date": "20150607" }, { "temperature": "22℃~34℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "西南风3-4 级", "week": "星期一", "date": "20150608" }, { "temperature": "22℃~33℃", "weather": "阴", "weather_id": { "fa": "02", "fb": "02" }, "wind": "西南风3-4 级", "week": "星期二", "date": "20150609" }, { "temperature": "22℃~33℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "南风3-4 级", "week": "星期三", "date": "20150610" } ] }, "error_code": 0 }
解析JSONObject
import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import com.google.gson.JsonIOException; import java.io.FileNotFoundException; import java.io.FileReader; public class ReadJson { public static void main(String []args) { JsonParser parse = new JsonParser(); try { JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json")); System.out.println("resultcode:" + json.get("resultcodeu").getAsInt()); System.out.println("reason:" + json.get("reason").getAsString()); JsonObject result = json.get("result").getAsJsonObject(); JsonObject today = result.get("today").getAsJsonObject(); System.out.println("weak:" + today.get("week").getAsString()); System.out.println("weather:" + today.get("weather").getAsString()); } catch (JsonIOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (JsonSyntaxException e){ e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
解析JSONArray
import com.google.gson.JsonParser; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import com.google.gson.JsonIOException; import java.io.FileNotFoundException; import java.io.FileReader; public class ReadJsonArray { public static void main(String []args) { JsonParser parse = new JsonParser(); try { JsonObject json = (JsonObject)parse.parse(new FileReader("C:\\Users\\wzh94434\\IdeaProjects\\TestProject\\jsontest\\src\\main\\java\\weather.json")); JsonObject result = json.get("result").getAsJsonObject(); JsonArray futureArray = result.get("future").getAsJsonArray(); for (int i = 0; i < futureArray.size(); ++i) { JsonObject subObj = futureArray.get(i).getAsJsonObject(); System.out.println("------"); System.out.println("week:" + subObj.get("week").getAsString()); System.out.println("weather:" + subObj.get("weather").getAsString()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } } }
注意:文件路径相对路径是从工程根目录开始
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- API——注册表读取
- VBA操作注册表
- VBA快速提取引用工程的代码
- NuGet 如何设置图标
- VBA解析VBAProject 00
- Echarts图表宽度变成100px,让图表宽度随着父元素自动适应,Vue实时监听宽度的变化,这可能是史上最好的解决方案!
- Vim 基础和常用命令整理
- TinyMCE 优化百度地图 bdmap 插件
- 更新!万字长文带你拿下九大排序的原理、Java 实现以及算法分析
- mysql 同一张表查询 left join
- uni-app运行到浏览器跨域H5页面的跨域问题解决方案
- 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇)
- 树状数据库表查询2次以上(自连接、内连接、别名)方法
- 网页背景H5视频自动播放---PC端、移动端兼容问题完美解决方案(IOS、安卓、微信端)
- 【STM32F407】第11章 RL-TCPnet V7.X之TCP服务器