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


猜你喜欢
- synchronized关键字,一般称之为”同步锁“,用它来修饰需要同步的方法和需要同步代码块,默认是当前对象作为锁的对象。同步锁锁的是同一
- 什么是反射机制反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作。例如它允许一个ja
- 一、java final基本概念:1、主要用于修饰类、属性和方法:被final修饰的类不可以被继承被final修饰的方法不可以被重写被fin
- 目录Map的computeIfAbsent使用场景和方法常规实现使用computeIfAbsent方法实现Map中computeIfAbse
- 下面分几部分介绍C#实现自动售货机接口的方法,代码写的非常详细,不懂的地方有注释可以参考下。MachineJP类:第1部分:串口初始化,串口
- 断言的概念断言用于证明和测试程序的假设,比如“这里的值大于 5”。断言可以在运行时从代码中完全删除,所以对代码的运行速度没有影响。断言的使用
- 开发环境安装JDK和JRE下载安装文件并安装:jdk-8u11-windows-i586.exejre-8u11-windows-i586.
- 本文借由并发环境下使用线程不安全的SimpleDateFormat优化案例,帮助大家理解ThreadLocal.最近整理公司项目,发现不少写
- 本文实例讲述了C#访问SqlServer设置链接超时的方法。分享给大家供大家参考。具体实现方法如下:下面这段代码设置超时时间为60秒,默认为
- 一、 测试代码:二、添加参数1、在终端工具中①先编译: javac Test.java②再运行: java Test args1 args2
- 最近工作的时候发现软件里面通过查询ARP表查询某一IP对应的ARP条目的时,概率性出现查询到的ARP条目为空,一开始怀疑Ping通但是没有学
- 中介者模式面向对象设计鼓励将行为分布到各个对象中, 这种分布可能会导致对象间有许多连接. 在最坏的情况下, 每一个对象都需要知道其他所有对象
- 一、简介SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入SpringBoot
- 1、反射的概念1、概念反射,指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对任意一个对象,都能调用它的任意一个方法。这种
- spring boot 使用POI读取Excel文件Excel文件目录Excel模板文件存了resourse目录下,如下图:<depe
- 在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的File
- 上一节初步了解了Android端的贝塞尔曲线,这一节就举个栗子练习一下,仿QQ未读消息气泡,是最经典的练习贝塞尔曲线的东东,效果如下附上gi
- 用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好;Android并没有给我们封装这样一个效果,我们来看下
- 1.引言在操作应用的时候,会有很多不同的手势操作,如按下、单击、双击、长按等手势,我们可以在这些手势事件中添加相应的业务逻辑,那么如何检测不
- 很多时候,我们需要使用C#中的WebClient 来收发数据,WebClient 类提供向 URI 标识的任何本地、Intranet 或 I