Java中利用gson解析Json实例教程
发布时间 - 2026-01-11 01:15:56 点击率:次前言

本文主要跟大家介绍了关于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();
}
}
}
注意:文件路径相对路径是从工程根目录开始
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
# java
# gson解析json
# java如何解析json
# JAVA使用Gson解析json数据实例解析
# 基于JAVA中的四种JSON解析方式详解
# 如何使用GSON解析JSON数据
# 滨州
# 相关内容
# 是从
# 朋友们
# 这篇文章
# 谢谢大家
# 看看吧
# 最弱
# 星期日
# 转阴
# 转晴
# 体弱者
# 有疑问
# city
# week
# date_y
# fa
# fb
# weather_id
# wind
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
详解jQuery停止动画——stop()方法的使用
ChatGPT常用指令模板大全 新手快速上手的万能Prompt合集
Laravel怎么实现验证码(Captcha)功能
Laravel如何发送系统通知_Laravel Notifications实现多渠道消息通知
网页设计与网站制作内容,怎样注册网站?
Laravel如何使用Sanctum进行API认证?(SPA实战)
Laravel怎么防止CSRF攻击_Laravel CSRF保护中间件原理与实践
EditPlus中的正则表达式实战(6)
如何用VPS主机快速搭建个人网站?
Laravel distinct去重查询_Laravel Eloquent去重方法
Python文件异常处理策略_健壮性说明【指导】
Laravel如何实现密码重置功能_Laravel密码找回与重置流程
Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
Laravel如何构建RESTful API_Laravel标准化API接口开发指南
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
JavaScript实现Fly Bird小游戏
制作电商网页,电商供应链怎么做?
Laravel如何自定义分页视图?(Pagination示例)
Laravel如何清理系统缓存命令_Laravel清除路由配置及视图缓存的方法【总结】
BootStrap整体框架之基础布局组件
如何在建站宝盒中设置产品搜索功能?
google浏览器怎么清理缓存_谷歌浏览器清除缓存加速详细步骤
Laravel如何使用.env文件管理环境变量?(最佳实践)
高性能网站服务器部署指南:稳定运行与安全配置优化方案
Laravel如何使用查询构建器?(Query Builder高级用法)
在Oracle关闭情况下如何修改spfile的参数
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
如何在阿里云香港服务器快速搭建网站?
Javascript中的事件循环是如何工作的_如何利用Javascript事件循环优化异步代码?
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
EditPlus中的正则表达式 实战(1)
在线制作视频网站免费,都有哪些好的动漫网站?
如何实现建站之星域名转发设置?
Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】
HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】
JavaScript常见的五种数组去重的方式
网站建设保证美观性,需要考虑的几点问题!
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
网站制作软件有哪些,制图软件有哪些?
Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
jquery插件bootstrapValidator表单验证详解
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
如何在阿里云虚拟服务器快速搭建网站?
Laravel如何优雅地处理服务层_在Laravel中使用Service层和Repository层
小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像
浅谈Javascript中的Label语句

