Java中利用gson解析Json实例教程
作者:jihite 发布时间:2023-10-31 04:07:41
标签: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();
}
}
}
注意:文件路径相对路径是从工程根目录开始
来源:http://www.cnblogs.com/kaituorensheng/p/6616126.html
0
投稿
猜你喜欢
- 一、场景描述建造者模式同工厂模式、抽象工厂模式一样,用于创建继承类对象。工厂模式:Java设计模式之工厂模式抽象工厂模式:Java设计模式之
- 这是一个可以从乱码文本中得到正确的原始文本的程序,其基于的原理在于错误的编码往往导致位补充,因此正确的文本使用的字节数应该是最少的(之一)。
- 引言在前两篇文章中,我们了解了ReentrantLock内部公平锁和非公平锁的实现原理,可以知道其底层基于AQS,使用双向链表实现,同时在线
- 错误详情:java.lang.NoSuchMethodException: [Lorg.springframework.web.multip
- java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java n
- 前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper 并实现多数据源和分页。其中SpringBo
- maven配置项目的jdk版本无效排查最近在配置项目的jdk的时候发现在pom.xml中配置的1.8版本无效,maven更新后就变成了1.7
- 题目描述:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数
- @SpringBootApplication的使用1. Spring Boot 的核心1.1. 入口类和 @SpringBootApplic
- 微信红包的使用已经很广泛,本篇文章介绍了微信发红包的实例,需要有认证的公众号,且开通了微信支付,商户平台且开通了现金红包的权限即可。http
- 1、什么是FeignFeign 是 Spring Cloud Netflix 组件中的一个轻量级 RESTful 的 HTTP 服务客户端,
- Spring JPA 增加字段执行异常用Spring jpa Entity里面增加了几个字段,但启动报错,提示column Unable t
- 一、 代码块的概念在探究对象初始化顺序之前,我们先通过代码来了解一下代码块的概念。class Test{ public stat
- 1.SQL注入:程序向后台数据库传递SQL时,用户提交的数据直接拼接到SQL语句中并执行,从而导入SQL注入攻击。字符型注入:黑色部分为拼接
- @Cacheable在同一类中方法调用无效上述图片中,同一个类中genLiveBullets()方法调用同类中的queryLiveByRoo
- 1.问题由来迷宫实验是取自心理学的一个古典实验。在该实验中,把一只老鼠从一个无顶大盒子的门放入,在盒中设置了许多墙,对行进方向形成了多处阻挡
- 1、继承Thread类方式这种方式适用于执行特定任务,并且需要获取处理后的数据的场景。举例:一个用于累加数组内数据的和的线程。public
- 一般表单数据分为两类<form method="post" action="${pageContext.
- 前言easyui是一种基于jQuery的用户界面插件集合。easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能。使用
- 目录Directory:创建文件夹删除文件夹获取文件夹下的子文件夹获取同类型的文件判断文件夹是否存在移动文件夹总结之前发过File对文件的操