欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

【JSON】Gson使用和踩坑

时间:2023-06-18
文章目录

序列化和反序列化测试

1.int2.double3.string4.Array5.Class6.List<> 结果展示:下节:Spring+gson/jackson 序列化和反序列化测试 1.int

@org.junit.jupiter.api.Test public void testInt(){ Gson gson=new Gson(); System.out.println("测试int序列化"+gson.toJson(1)); Integer number1=gson.fromJson("1", Integer.class); int number=gson.fromJson("265",int.class); }

2.double

@org.junit.jupiter.api.Test public void testDouble(){ Gson gson=new Gson(); System.out.println("测试double序列化"+gson.toJson(10.156)); System.out.println("测试double反序列化"+gson.fromJson("123.00",Double.class)); }

3.string

@org.junit.jupiter.api.Test public void testString(){ Gson gson=new Gson(); System.out.println("测试字符串序列化"+gson.toJson("ggzx")); System.out.println("测试字符串反序列化"+gson.fromJson(gson.toJson("ggzx",String.class))); }

4.Array

@org.junit.jupiter.api.Test public void testArray(){ Gson gson=new Gson(); int[] values={1,2,5,898,4}; System.out.println("测试数组序列化"+gson.toJson(values)); System.out.println("测试数组反序列化"); int[] values1=gson.fromJson(gson.toJson(values),int[].class); for (int a:values1) { System.out.print(a); } }

5.Class

@org.junit.jupiter.api.Test public void testClass(){ Gson gson=new Gson(); User user=new User("123","send-data",12,"123456",56.0); System.out.println("测试对象序列化"+gson.toJson(user)); System.out.println("测试对象反序列化"+user); }

6.List<>

@org.junit.jupiter.api.Test public void testClassList(){ Gson gson=new Gson(); User user1=new User("123","send-data",12,"123456",56.0); User user2=new User("123","send-data",12,"123456",56.0); User user3=new User("123","send-data",12,"123456",56.0); List userList=new linkedList<>(); userList.add(user1); userList.add(user2); userList.add(user3); System.out.println("测试List<>序列化:"+gson.toJson(userList)); Type userListType = new TypeToken>(){}.getType(); List userList1=gson.fromJson(gson.toJson(userList),userListType); System.out.println("测试List<>反序列化"+gson.fromJson(gson.toJson(userList),userListType)); for (User user:userList1) { System.out.println(user.toString()); } }

Map

@org.junit.jupiter.api.Test public void testMap(){ Gson gson=new Gson(); Map map=new HashMap<>(); map.put("user","ggzx"); map.put("password","123"); System.out.println("测试map序列化"+gson.toJson(map)); Type mapType = new TypeToken>(){}.getType(); System.out.println("测试map反序列化"+gson.fromJson(gson.toJson(map),mapType)); }

8.复杂对象

@org.junit.jupiter.api.Test public void testComplexClass(){ Gson gson=new Gson(); Zoo zoo=new Zoo(); ZooKeeper zooKeeper=new ZooKeeper("ggzx",18); zoo.setZooKeeper(zooKeeper); Lion lion=new Lion("lion1",3); Lion lion2=new Lion("lion2",5); Lion lion3=new Lion("lion3",5); List lionList=new linkedList<>(); lionList.add(lion); lionList.add(lion2); lionList.add(lion3); Monkey monkey=new Monkey("monkey1",7); Monkey monkey2=new Monkey("monkey2",8); Monkey monkey3=new Monkey("monkey3",9); List monkeyList=new linkedList<>(); monkeyList.add(monkey); monkeyList.add(monkey2); monkeyList.add(monkey3); zoo.setLionList(lionList); zoo.setMonkeyList(monkeyList); System.out.println("测试序列化复杂对象"+gson.toJson(zoo)); System.out.println("测试复杂对象反序列化"+gson.fromJson(gson.toJson(zoo),Zoo.class)); }

结果展示:

下面分类进行测试序列化和反序列化=======>>>>测试int序列化1测试double序列化10.156测试double反序列化123.0测试数组序列化[1,2,5,898,4]测试数组反序列化1258984测试字符串序列化"ggzx"测试字符串反序列化ggzx测试对象序列化{"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0}测试对象反序列化User{code='123', mode='send-data', age=12, password='123456', salary=56.0}测试List<>序列化:[{"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0},{"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0},{"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0}]测试List<>反序列化[User{code='123', mode='send-data', age=12, password='123456', salary=56.0}, User{code='123', mode='send-data', age=12, password='123456', salary=56.0}, User{code='123', mode='send-data', age=12, password='123456', salary=56.0}]User{code='123', mode='send-data', age=12, password='123456', salary=56.0}User{code='123', mode='send-data', age=12, password='123456', salary=56.0}User{code='123', mode='send-data', age=12, password='123456', salary=56.0}测试map序列化{"password":"123","user":"ggzx"}测试map反序列化{password=123, user=ggzx}测试序列化复杂对象{"zooKeeper":{"name":"ggzx","age":18},"lionList":[{"name":"lion1","age":3},{"name":"lion2","age":5},{"name":"lion3","age":5}],"monkeyList":[{"name":"monkey1","age":7},{"name":"monkey2","age":8},{"name":"monkey3","age":9}]}测试复杂对象反序列化Zoo{zooKeeper=ZooKeeper{name='ggzx', age=18}, lionList=[Lion{name='lion1', age=3}, Lion{name='lion2', age=5}, Lion{name='lion3', age=5}], monkeys=[Monkey{name='monkey1', age=7}, Monkey{name='monkey2', age=8}, Monkey{name='monkey3', age=9}]}

9.自定义对象
如果想使用毕竟复杂的对象,比如
user:存储用户信息
dataList:存放很多种类的List<>数据,
我们可以把这两个对象封装在同一个类中,比如封装成Data类
在初始化数据之后,直接将其gson.toJson即可;
在接收的时候,也只需要gons.fromJson(json,Data.class);

下节:Spring+gson/jackson

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。