Gson之toJson和fromJson方法的具体使用
作者:Archie_java 时间:2021-07-20 16:28:47
Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。
Gson里最重要的对象有2个Gson 和 GsonBuilder
Gson有2个最基本的方法
toJson() – 转换java 对象到JSON
fromJson() – 转换JSON到java对象
引入依赖:pom.xml文件中加入
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
1.toJson()方法是实现从java实体到Json相关对象的方法
(1)将对象转换为json字符串
Gson gson =new Gson();
User user = new User(123,"zy");
String str = gson.toJson(user);
System.out.println(str);
输出结果为
(2)将map集合转变为json字符串
Gson gson1 =new Gson();
Map<Integer, String> map= new HashMap<Integer, String>();
map.put(11, "zy");
map.put(12, "zz");
String str1 = gson1.toJson(map);
System.out.println(str1);
输出结果为
2.fromJson()方法来实现从Json相关对象到java实体的方法
(1)将json字符串转换为java对象
Gson gson =new Gson();
User user = new User(123,"zy");
//将对象转为json字符串
String str = gson.toJson(user);
//再由json字符串转为java对象,通过get方法得到对象里的值
User fromJson = gson.fromJson(str, User.class);
System.out.println(fromJson.getAge()+"......"+fromJson.getName());
输出结果为
(2)将json字符串转换为map集合
Gson gson =new Gson();
Map<Integer, String> map= new HashMap<Integer, String>();
map.put(11, "zy");
map.put(12, "zz");
//将map集合转换为json字符串
String str = gson.toJson(map);
//将json字符串转换为map集合
Type type = new TypeToken<Map<Integer, String>>() {
}.getType();
Map<Integer, String> map1 = gson.fromJson(str, type);
//遍历map集合
for (Integer key : map1.keySet()) {
System.out.println("key="+key+"\tvalue="+map1.get(key));
}
输出结果为
来源:https://blog.csdn.net/qq_43842093/article/details/121258638
标签:Gson,toJson,fromJson
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于JAVA11中图片与BASE64相互转换的实现
2022-12-06 06:44:11
![](https://img.aspxhome.com/file/2023/6/73086_0s.png)
Java List分页功能实现代码实例
2022-06-02 13:56:14
关于eclipse中运行tomcat提示端口被占用的4种解决
2022-04-15 10:56:12
登陆验证码kaptcha结合spring boot的用法详解
2023-02-19 15:56:32
java中Memcached的使用实例(包括与Spring整合)
2021-11-28 13:29:06
教你怎么在IDEA中创建java多模块项目
2023-05-28 19:25:58
![](https://img.aspxhome.com/file/2023/9/65199_0s.png)
android View 绘制完成监听的实现方法
2023-08-06 20:47:05
Spring深入刨析声明式事务注解的源码
2023-10-23 09:41:48
![](https://img.aspxhome.com/file/2023/6/61026_0s.png)
java网络编程之socket网络编程示例(服务器端/客户端)
2022-05-02 00:18:59
![](https://img.aspxhome.com/file/2023/7/72587_0s.jpg)
Java即将引入新对象类型来解决内存使用问题
2023-03-30 18:12:42
![](https://img.aspxhome.com/file/2023/5/70935_0s.png)
JDK集合源码之解析TreeMap(一)
2021-08-09 07:14:02
![](https://img.aspxhome.com/file/2023/3/66643_0s.png)
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
2023-11-26 15:05:59
Spring Boot 员工管理系统超详细教程(源码分享)
2022-04-18 13:32:26
![](https://img.aspxhome.com/file/2023/5/68565_0s.jpg)
Java使用Optional实现优雅避免空指针异常
2023-06-05 15:35:39
浅谈java常量池
2023-11-18 09:43:50
如何将写好的.py/.java程序变成.exe文件详解
2022-04-06 09:22:14
![](https://img.aspxhome.com/file/2023/1/70161_0s.png)
Idea中maven项目实现登录验证码功能
2023-11-29 16:50:51
![](https://img.aspxhome.com/file/2023/5/60575_0s.png)
SpringBoot读取资源目录中JSON文件的方法实例
2023-04-26 02:00:42
![](https://img.aspxhome.com/file/2023/6/69136_0s.jpg)
SpringBoot中Dozer的使用小结
2023-11-25 01:24:38
浅谈java中守护线程与用户线程
2023-11-26 20:46:41
![](https://img.aspxhome.com/file/2023/3/60173_0s.png)