java对象和json转换三种工具类汇总
说明:
去年的时候就像总结json工具类的,不然每次用到都需要百度,好麻烦,这次总结了三种工具类详细的用法
准备工作
1.准备springboot工程
2.引入依赖
jackson是springboot默认json转换方式的工具类
Gson,是google的json工具类,我们需要自己引入
FastJson是阿里的json工具类,需要问哦们手动引入
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!--springfox swagger官方Starter-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--引入开源工具库hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.demo.DemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.准备测试的java bean
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Demo {
private Integer id;
private String userName;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date bir;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DemoOne {
private Integer id;
private String userName;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date bir;
private Double salary;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DemoThree {
private Integer id;
private String userName;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date bir;
private Double salary;
private List<Demo> demoList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class DemoTwo {
private Integer id;
private String userName;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date bir;
private Double salary;
private Demo demo;
}
Jackson测试
import cn.hutool.core.bean.BeanUtil;
import com.example.demo.model.Demo;
import com.example.demo.model.DemoOne;
import com.example.demo.model.DemoThree;
import com.example.demo.model.DemoTwo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
/**
* 测试jackson工具类ObjectMapper的api
* json和java对象互相转换
*/
@SpringBootTest
public class JacksonTest {
/**
* 获取jackson进行java和json转换的工具类ObjectMapper
*/
public ObjectMapper getObjMapper() {
return new ObjectMapper();
}
public List<DemoOne> getList() {
List<DemoOne> demoOneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("userName--" + i);
demoOneList.add(demoOne);
}
return demoOneList;
}
public Map<String, Object> getMap() {
HashMap<String, Object> map = new HashMap<>();
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
BeanUtil.copyProperties(demoOne, map);
return map;
}
public List<Map<String, Object>> getListMap() {
List<Map<String, Object>> listMap = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("zhangsan--" + i);
HashMap<String, Object> map = new HashMap<>();
BeanUtil.copyProperties(demoOne, map);
listMap.add(map);
}
return listMap;
}
/**
* 获取测试java bean , 并且这个java bean属性有java bean
*
* @return
*/
public DemoTwo getJavaBeanFiledJavaBean() {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfJavaBeanUserName");
DemoTwo demoTwo = new DemoTwo().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemo(demo);
return demoTwo;
}
/**
* 获取测试java bean , 并且这个java bean属性有 list
*
* @return
*/
public DemoThree getJavaBeanFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
return demoThree;
}
/**
* 获取测试list , 并且这个list属性有 list
*
* @return
*/
public List<DemoThree> getListFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
List<DemoThree> demoThreeList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
for (int i = 0; i < 5; i++) {
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
demoThreeList.add(demoThree);
}
return demoThreeList;
}
/**
* java bean 转为json格式字符窜
*
* @throws JsonProcessingException
*/
@Test
public void testJavaBeanToJson() throws JsonProcessingException {
ObjectMapper objMapper = getObjMapper();
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
String demoOneToJsonString = objMapper.writeValueAsString(demoOne);
System.out.println(demoOneToJsonString);
}
/**
* json格式字符窜转为 java bean
*/
@Test
public void testJsonToJavaBean() throws JsonProcessingException {
String jsonString = "{\"id\":1,\"userName\":\"张三\",\"bir\":\"2021-04-25 09:58:53\",\"salary\":10000.0}";
ObjectMapper objMapper = getObjMapper();
DemoOne demoOne = objMapper.readValue(jsonString, DemoOne.class);
System.out.println(demoOne);
}
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testListBaseDataToJsonString() throws JsonProcessingException {
List<String> listString = new ArrayList<>();
listString.add("AAAA");
listString.add("BBBB");
listString.add("CCCC");
ObjectMapper objMapper = getObjMapper();
String stringToJsonString = objMapper.writeValueAsString(listString);
System.out.println(stringToJsonString);
}
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testJsonStringToListString() throws JsonProcessingException {
ObjectMapper objMapper = getObjMapper();
String stringJsonString = "[\"AAAA\",\"BBBB\",\"CCCC\"]";
List list = objMapper.readValue(stringJsonString, List.class);
System.out.println("打印list<String>集合的数据");
System.out.println(list);
String strIndex1 = (String) list.get(1);
System.out.println("List<String>中索引为1的值");
System.out.println(strIndex1);
}
/**
* java中list集合转为json格式字符窜
*/
@Test
public void testListToJson() throws JsonProcessingException {
List<DemoOne> demoOneList = getList();
ObjectMapper objMapper = getObjMapper();
String listJsonString = objMapper.writeValueAsString(demoOneList);
System.out.println(listJsonString);
}
/**
* json格式字符窜转换为java list<T> T为自定义实体类
* 报错
* java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.demo.model.DemoOne
*/
@Test
public void testJsonToListWrong() throws JsonProcessingException {
String listJsonString = "[{\"id\":1,\"userName\":\"userName--0\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--1\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--2\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--3\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--4\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0}]\n";
ObjectMapper objMapper = getObjMapper();
List list = objMapper.readValue(listJsonString, List.class);
System.out.println("打印出List<DemoOne>数据");
DemoOne demoOne = (DemoOne) list.get(1);
System.out.println("打印出List<DemoOne>集合索引为1的数据");
System.out.println(demoOne);
}
/**
* json格式字符窜转换为java list<T> T为自定义实体类
*/
@Test
public void testJsonToList() throws JsonProcessingException {
String listJsonString = "[{\"id\":1,\"userName\":\"userName--0\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--1\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--2\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--3\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--4\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0}]\n";
ObjectMapper objMapper = getObjMapper();
/**
* 注意此处引入的是
* import com.fasterxml.jackson.core.type.TypeReference;
*/
/**
* 使用匿名内部类方式声明TypeReference
*/
TypeReference<List<DemoOne>> listDemoTypeReference = new TypeReference<List<DemoOne>>() {
};
/**
* 使用自定义TypeReference方式指明json字符窜转换后的数据类型
*/
ListDemoOneTypeReference mylistDemoOneTypeReference = new ListDemoOneTypeReference();
List<DemoOne> list = objMapper.readValue(listJsonString, listDemoTypeReference);
List<DemoOne> mylist = objMapper.readValue(listJsonString, mylistDemoOneTypeReference);
System.out.println("方式一:打印出List<DemoOne>数据");
System.out.println(list);
System.out.println("方式二:打印出List<DemoOne>数据");
System.out.println(mylist);
DemoOne demoOne = list.get(1);
DemoOne mydemoOne = mylist.get(1);
System.out.println("方式一:打印出List<DemoOne>集合索引为1的数据");
System.out.println(demoOne);
System.out.println("方式二:打印出List<DemoOne>集合索引为1的数据");
System.out.println(mydemoOne);
}
/**
* java bean中属性有java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledJavaBeanToJsonString() throws JsonProcessingException {
DemoTwo demoTwo = getJavaBeanFiledJavaBean();
System.out.println("javabean信息");
System.out.println(demoTwo);
System.out.println("转为json后的数据");
ObjectMapper objMapper = getObjMapper();
String jsonString = objMapper.writeValueAsString(demoTwo);
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有java bean
*/
@Test
public void testJsonStringToJavaBeanFiledJavaBean() throws JsonProcessingException {
String jsonString = "{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 10:43:55\"," +
"\"salary\":10000.0,\"demo\":{\"id\":1,\"userName\":\"filedOfJavaBeanUserName\",\"bir\":\"2021-04-25 " +
"10:43:55\"}}\n";
ObjectMapper objMapper = getObjMapper();
DemoTwo demoTwo = objMapper.readValue(jsonString, new TypeReference<DemoTwo>() {
});
System.out.println("json格式字符窜转为java对象的数据:");
System.out.println(demoTwo);
System.out.println("json格式字符窜转为java对象中属性为javabean属性的数据:");
System.out.println(demoTwo.getDemo());
}
/**
* java bean中属性有List<T> t为自定义java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledListToJsonString() throws JsonProcessingException {
DemoThree demoThree = getJavaBeanFiledList();
ObjectMapper objMapper = getObjMapper();
String jsonString = objMapper.writeValueAsString(demoThree);
System.out.println("javabean信息");
System.out.println(demoThree);
System.out.println("转为json后的数据");
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有List<T> t为自定义java bean
*/
@Test
public void testJsonStringToJavaBeanFiledList() throws JsonProcessingException {
String jsonString = "{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 10:51:48\"," +
"\"salary\":10000" +
".0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\",\"bir\":\"2021-04-25 10:51:48\"}," +
"{\"id\":1,\"userName\":\"filedOfListName-----1\",\"bir\":\"2021-04-25 10:51:48\"},{\"id\":1," +
"\"userName\":\"filedOfListName-----2\",\"bir\":\"2021-04-25 10:51:48\"},{\"id\":1," +
"\"userName\":\"filedOfListName-----3\",\"bir\":\"2021-04-25 10:51:48\"},{\"id\":1," +
"\"userName\":\"filedOfListName-----4\",\"bir\":\"2021-04-25 10:51:48\"}]}\n";
ObjectMapper objMapper = getObjMapper();
DemoThree demoThree = objMapper.readValue(jsonString, new TypeReference<DemoThree>() {
});
System.out.println("转换后的javabean信息");
System.out.println(demoThree);
System.out.println("转换后的javabean中list中信息");
System.out.println(demoThree.getDemoList());
System.out.println("转换后的javabean中list中索引为2的javabean信息");
System.out.println(demoThree.getDemoList().get(2));
}
/**
* list属性有 list 转为 json格式字符窜
*/
@Test
public void testlistFiledListToJsonString() throws JsonProcessingException {
List<DemoThree> listFiledList = getListFiledList();
ObjectMapper objMapper = getObjMapper();
String stringJson = objMapper.writeValueAsString(listFiledList);
System.out.println("list信息");
System.out.println(listFiledList);
System.out.println("转为json后的数据");
System.out.println(stringJson);
}
/**
* json字符窜 转为 list 属性 中有list
*/
@Test
public void testJsonStringTolistFiledList() throws JsonProcessingException {
String jsonString = "[{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 11:22:26\"," +
"\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]}]\n";
ObjectMapper objMapper = getObjMapper();
List<DemoThree> demoThreeList = objMapper.readValue(jsonString, new TypeReference<List<DemoThree>>() {
});
System.out.println("转换后的list信息");
System.out.println(demoThreeList);
System.out.println("转换后的list索引为2信息");
System.out.println(demoThreeList.get(2));
System.out.println("转换后的list索引为2属性中list中信息");
System.out.println(demoThreeList.get(2).getDemoList());
System.out.println("转换后的list索引为2属性中list索引为2信息");
System.out.println(demoThreeList.get(2).getDemoList().get(2));
}
/**
* map 转换为 json格式字符窜
*/
@Test
public void testMapToJsonString() throws JsonProcessingException {
Map<String, Object> map = getMap();
ObjectMapper objMapper = getObjMapper();
String jsonString = objMapper.writeValueAsString(map);
System.out.println("map信息");
System.out.println(map);
System.out.println("map转为json格式后:");
System.out.println(jsonString);
}
/**
* json 格式转map
*/
@Test
public void testJsonStringToMap() throws JsonProcessingException {
String jsonString = "{\"bir\":1619328801393,\"id\":1,\"userName\":\"张三\",\"salary\":10000.0}";
ObjectMapper objMapper = getObjMapper();
HashMap map = objMapper.readValue(jsonString, HashMap.class);
System.out.println("json格式字符串转map对象");
System.out.println(map);
}
/**
*list<map> 转为 json格式的字符窜
*/
@Test
public void testListMapToJsonString() throws JsonProcessingException {
List<Map<String, Object>> listMap = getListMap();
ObjectMapper objMapper = getObjMapper();
String jsonString = objMapper.writeValueAsString(listMap);
System.out.println("list<map>的数据:");
System.out.println(listMap);
System.out.println("list<map>转为json字符窜的数据:");
System.out.println(jsonString);
}
/**
* json格式字符窜 转为 list<map>
*/
@Test
public void testJsonStringToListMap() throws JsonProcessingException {
String jsonString = "[{\"bir\":1619329384863,\"id\":1,\"userName\":\"zhangsan--0\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--1\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--2\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--3\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--4\",\"salary\":10000.0}]\n";
ObjectMapper objMapper = getObjMapper();
List<Map> listMap = objMapper.readValue(jsonString, new TypeReference<List<Map>>() {
});
System.out.println("json格式字符窜转为list<map>");
System.out.println(listMap);
}
}
需要的别的辅助类
/**
* @author shaoming
* @Date: 2021/4/25 09:24
* @Description:
* Jackson声明自定义TypeReference指明json转为java对象的数据类型
*/
public class ListDemoOneTypeReference extends TypeReference<List<DemoOne>> {
}
FastJson测试
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.example.demo.model.Demo;
import com.example.demo.model.DemoOne;
import com.example.demo.model.DemoThree;
import com.example.demo.model.DemoTwo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.*;
/**
* 测试fastjson工具类JSON的api
* json和java对象互相转换
*/
public class FastJsonTest {
public List<DemoOne> getList() {
List<DemoOne> demoOneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("userName--" + i);
demoOneList.add(demoOne);
}
return demoOneList;
}
public Map<String, Object> getMap() {
HashMap<String, Object> map = new HashMap<>();
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
BeanUtil.copyProperties(demoOne, map);
return map;
}
public List<Map<String, Object>> getListMap() {
List<Map<String, Object>> listMap = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("zhangsan--" + i);
HashMap<String, Object> map = new HashMap<>();
BeanUtil.copyProperties(demoOne, map);
listMap.add(map);
}
return listMap;
}
/**
* 获取测试java bean , 并且这个java bean属性有java bean
*
* @return
*/
public DemoTwo getJavaBeanFiledJavaBean() {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfJavaBeanUserName");
DemoTwo demoTwo = new DemoTwo().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemo(demo);
return demoTwo;
}
/**
* 获取测试java bean , 并且这个java bean属性有 list
*
* @return
*/
public DemoThree getJavaBeanFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
return demoThree;
}
/**
* 获取测试list , 并且这个list属性有 list
*
* @return
*/
public List<DemoThree> getListFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
List<DemoThree> demoThreeList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
for (int i = 0; i < 5; i++) {
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
demoThreeList.add(demoThree);
}
return demoThreeList;
}
/**
* java bean 转为json格式字符窜
*
* @throws JsonProcessingException
*/
@Test
public void testJavaBeanToJson() {
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
System.out.println("java bean转为json格式字符窜");
String JsonString = JSON.toJSONString(demoOne);
System.out.println(JsonString);
System.out.println("java bean转为json格式字符窜,使用PrettyFormat(true表示格式化json格式)");
String jsonStringPrettyFormat = JSON.toJSONString(demoOne, true);
System.out.println(jsonStringPrettyFormat);
}
/**
* json格式字符窜转为 java bean
*/
@Test
public void testJsonToJavaBean() throws JsonProcessingException {
String jsonString = "{\"bir\":1619329956814,\"id\":1,\"salary\":10000.0,\"userName\":\"张三\"}";
DemoOne demoOne = JSON.parseObject(jsonString, DemoOne.class);
System.out.println("json字符窜转为java bean");
System.out.println(demoOne);
}
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testListBaseDataToJsonString() throws JsonProcessingException {
List<String> listString = new ArrayList<>();
listString.add("AAAA");
listString.add("BBBB");
listString.add("CCCC");
String jsonString = JSON.toJSONString(listString, true);
System.out.println("list<string>转为json格式字符窜,使用PrettyFormat(true表示格式化json格式)");
System.out.println(jsonString);
}
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testJsonStringToListString() throws JsonProcessingException {
String jsonString = "[\"AAAA\",\"BBBB\",\"CCCC\"]";
List<String> stringList
= JSON.parseArray(jsonString, String.class);
System.out.println("打印list<String>集合的数据");
System.out.println(stringList);
String strIndex1 = (String) stringList.get(1);
System.out.println("List<String>中索引为1的值");
System.out.println(strIndex1);
}
/**
* List<T> T为java bean 转为json格式字符窜
*/
@Test
public void testListToJson() throws JsonProcessingException {
List<DemoOne> demoOneList = getList();
String jsonString = JSON.toJSONString(demoOneList);
System.out.println("List<T> T为javabean 转 json格式字符窜");
System.out.println(jsonString);
}
/**
* json格式字符窜转换为java list<T> T为自定义实体类
* 报错
* java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.demo.model.DemoOne
* 如果json数据为集合形式,那么我们需要调用JSON.ParaseArray(String , class)这个api
* 而不是JSON.ParseObject(String,class)这个api
*/
@Test
public void testJsonToListWrong() throws JsonProcessingException {
String jsonString = "[{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--0\"}," +
"{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--1\"}," +
"{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--2\"}," +
"{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--3\"}," +
"{\"bir\":1619330606019,\"id\":1,\"salary\":10000.0,\"userName\":\"userName--4\"}]\n";
List list = JSON.parseObject(jsonString, List.class);
System.out.println("打印出List<DemoOne>数据");
DemoOne demoOne = (DemoOne) list.get(1);
System.out.println("打印出List<DemoOne>集合索引为1的数据");
System.out.println(demoOne);
}
/**
* json格式字符窜转换为java list<T> T为自定义实体类
* 说明:
* 如果json字符窜是集合形式,我们需要使用JSON.ParseArray(String jsonString,Class)这个api
*/
@Test
public void testJsonToList() {
String jsonString = "[{\"id\":1,\"userName\":\"userName--0\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--1\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--2\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--3\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--4\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0}]\n";
List<DemoOne> demoOneList = JSON.parseArray(jsonString, DemoOne.class);
System.out.println("json格式字符窜转为List<T> T为java bean");
System.out.println(demoOneList);
}
/**
* java bean中属性有java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledJavaBeanToJsonString() throws JsonProcessingException {
DemoTwo demoTwo = getJavaBeanFiledJavaBean();
System.out.println("javabean信息");
System.out.println(demoTwo);
System.out.println("转为json后的数据");
String jsonString = JSON.toJSONString(demoTwo);
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有java bean
*/
@Test
public void testJsonStringToJavaBeanFiledJavaBean() throws JsonProcessingException {
String jsonString = "{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 10:43:55\"," +
"\"salary\":10000.0,\"demo\":{\"id\":1,\"userName\":\"filedOfJavaBeanUserName\",\"bir\":\"2021-04-25 " +
"10:43:55\"}}\n";
System.out.println("json格式字符窜转为java对象的数据:");
DemoTwo demoTwo = JSON.parseObject(jsonString, DemoTwo.class);
System.out.println(demoTwo);
System.out.println("json格式字符窜转为java对象中属性为javabean属性的数据:");
System.out.println(demoTwo.getDemo());
}
/**
* java bean中属性有List<T> t为自定义java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledListToJsonString() throws JsonProcessingException {
DemoThree demoThree = getJavaBeanFiledList();
String jsonString = JSON.toJSONString(demoThree);
System.out.println("javabean信息");
System.out.println(demoThree);
System.out.println("转为json后的数据");
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有List<T> t为自定义java bean
*/
@Test
public void testJsonStringToJavaBeanFiledList() throws JsonProcessingException {
String jsonString = "{\"bir\":1619331360905,\"demoList\":[{\"bir\":1619331360905,\"id\":1," +
"\"userName\":\"filedOfListName-----0\"},{\"bir\":1619331360905,\"id\":1," +
"\"userName\":\"filedOfListName-----1\"},{\"bir\":1619331360905,\"id\":1," +
"\"userName\":\"filedOfListName-----2\"},{\"bir\":1619331360905,\"id\":1," +
"\"userName\":\"filedOfListName-----3\"},{\"bir\":1619331360905,\"id\":1," +
"\"userName\":\"filedOfListName-----4\"}],\"id\":1,\"salary\":10000.0,\"userName\":\"javabeanOfNae\"}\n";
DemoThree demoThree = JSON.parseObject(jsonString, DemoThree.class);
System.out.println("转换后的javabean信息");
System.out.println(demoThree);
System.out.println("转换后的javabean中list中信息");
System.out.println(demoThree.getDemoList());
System.out.println("转换后的javabean中list中索引为2的javabean信息");
System.out.println(demoThree.getDemoList().get(2));
}
/**
* list属性有 list 转为 json格式字符窜
*/
@Test
public void testlistFiledListToJsonString() {
List<DemoThree> listFiledList = getListFiledList();
String jsonString = JSON.toJSONString(listFiledList);
System.out.println("list信息");
System.out.println(listFiledList);
System.out.println("转为json后的数据");
System.out.println(jsonString);
}
/**
* json字符窜 转为 list 属性 中有list
*/
@Test
public void testJsonStringTolistFiledList() {
String jsonString = "[{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 11:22:26\"," +
"\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]}]\n";
List<DemoThree> demoThreeList = JSON.parseArray(jsonString, DemoThree.class);
System.out.println("转换后的list信息");
System.out.println(demoThreeList);
System.out.println("转换后的list索引为2信息");
System.out.println(demoThreeList.get(2));
System.out.println("转换后的list索引为2属性中list中信息");
System.out.println(demoThreeList.get(2).getDemoList());
System.out.println("转换后的list索引为2属性中list索引为2信息");
System.out.println(demoThreeList.get(2).getDemoList().get(2));
}
/**
* map 转换为 json格式字符窜
*/
@Test
public void testMapToJsonString() {
Map<String, Object> map = getMap();
String jsonString = JSON.toJSONString(map);
System.out.println("map信息");
System.out.println(map);
System.out.println("map转为json格式后:");
System.out.println(jsonString);
}
/**
* json 格式转map
*/
@Test
public void testJsonStringToMap() throws JsonProcessingException {
String jsonString = "{\"bir\":1619328801393,\"id\":1,\"userName\":\"张三\",\"salary\":10000.0}";
Map map = JSON.parseObject(jsonString, Map.class);
System.out.println("json格式字符串转map对象");
System.out.println(map);
}
/**
* list<map> 转为 json格式的字符窜
*/
@Test
public void testListMapToJsonString() throws JsonProcessingException {
List<Map<String, Object>> listMap = getListMap();
String jsonString = JSON.toJSONString(listMap);
System.out.println("list<map>的数据:");
System.out.println(listMap);
System.out.println("list<map>转为json字符窜的数据:");
System.out.println(jsonString);
}
/**
* json格式字符窜 转为 list<map>
*/
@Test
public void testJsonStringToListMap() {
String jsonString = "[{\"bir\":1619329384863,\"id\":1,\"userName\":\"zhangsan--0\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--1\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--2\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--3\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--4\",\"salary\":10000.0}]\n";
List<Map> listMap = JSON.parseArray(jsonString, Map.class);
System.out.println("json格式字符窜转为list<map>");
System.out.println(listMap);
System.out.println("获取list集合第一个元素的map,再map中key为id的value的值");
Map map = (Map) listMap.get(1);
System.out.println(map);
Object id = map.get("id");
System.out.println(id);
}
}
Gson测试
package com.example.demo.json;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.example.demo.model.Demo;
import com.example.demo.model.DemoOne;
import com.example.demo.model.DemoThree;
import com.example.demo.model.DemoTwo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
/**
* 测试GSON工具类GSON的api
* json和java对象互相转换
*/
@SpringBootTest
public class GsonTest {
public Gson getGson() {
return new Gson();
}
public Gson getDateFormatGson() {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); //按照 yyyy-MM-dd HH:mm:ss格式化。
return gson;
}
public List<DemoOne> getList() {
List<DemoOne> demoOneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("userName--" + i);
demoOneList.add(demoOne);
}
return demoOneList;
}
public Map<String, Object> getMap() {
HashMap<String, Object> map = new HashMap<>();
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
BeanUtil.copyProperties(demoOne, map);
return map;
}
public List<Map<String, Object>> getListMap() {
List<Map<String, Object>> listMap = new ArrayList<>();
for (int i = 0; i < 5; i++) {
DemoOne demoOne =
new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("zhangsan--" + i);
HashMap<String, Object> map = new HashMap<>();
BeanUtil.copyProperties(demoOne, map);
listMap.add(map);
}
return listMap;
}
/**
* 获取测试java bean , 并且这个java bean属性有java bean
*
* @return
*/
public DemoTwo getJavaBeanFiledJavaBean() {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfJavaBeanUserName");
DemoTwo demoTwo = new DemoTwo().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemo(demo);
return demoTwo;
}
/**
* 获取测试java bean , 并且这个java bean属性有 list
*
* @return
*/
public DemoThree getJavaBeanFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
return demoThree;
}
/**
* 获取测试list , 并且这个list属性有 list
*
* @return
*/
public List<DemoThree> getListFiledList() {
List<Demo> demoList = new ArrayList<Demo>();
List<DemoThree> demoThreeList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo().setBir(new Date()).setId(1).setUserName("filedOfListName-----" + i);
demoList.add(demo);
}
for (int i = 0; i < 5; i++) {
DemoThree demoThree = new DemoThree().setId(1).setBir(new Date()).setSalary(10000.0).setUserName(
"javabeanOfNae").setDemoList(demoList);
demoThreeList.add(demoThree);
}
return demoThreeList;
}
/**
* java bean 转为json格式字符窜
*
* @throws JsonProcessingException
*/
@Test
public void testJavaBeanToJson() {
DemoOne demoOne = new DemoOne().setBir(new Date()).setId(1).setSalary(10000.0).setUserName("张三");
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); //按照 yyyy-MM-dd HH:mm:ss格式化。Gson
/**
* 如果gson不进行时间格式化处理可能会报错
* com.google.gson.JsonSyntaxException
*/
// Gson gson = new Gson();
String jsonString = gson.toJson(demoOne);
System.out.println("java bean转为json格式字符窜");
System.out.println(jsonString);
}
/*
控制台打印:
java bean转为json格式字符窜
{"id":1,"userName":"张三","bir":"2021-04-25 15:40:31","salary":10000.0}
*/
/**
* json格式字符窜转为 java bean
*/
@Test
public void testJsonToJavaBean() {
String jsonString = "{\"id\":1,\"userName\":\"张三\",\"bir\":\"2021-04-25 15:36:49\",\"salary\":10000.0}";
Gson gson = getGson();
DemoOne demoOne = gson.fromJson(jsonString, DemoOne.class);
System.out.println("json字符窜转为java bean");
System.out.println(demoOne);
}
/*
json字符窜转为java bean
DemoOne(id=1, userName=张三, bir=Sun Apr 25 15:36:49 CST 2021, salary=10000.0)
*/
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testListBaseDataToJsonString() throws JsonProcessingException {
List<String> listString = new ArrayList<>();
listString.add("AAAA");
listString.add("BBBB");
listString.add("CCCC");
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(listString);
System.out.println("list<string>转为json格式字符窜,使用PrettyFormat(true表示格式化json格式)");
System.out.println(jsonString);
}
/**
* java中list<T> T为基础数据类型 转为json
*/
@Test
public void testJsonStringToListString() throws JsonProcessingException {
String jsonString = "[\"AAAA\",\"BBBB\",\"CCCC\"]";
Gson gson = getDateFormatGson();
List stringList = gson.fromJson(jsonString, List.class);
System.out.println("打印list<String>集合的数据");
System.out.println(stringList);
String strIndex1 = (String) stringList.get(1);
System.out.println("List<String>中索引为1的值");
System.out.println(strIndex1);
}
/**
* List<T> T为java bean 转为json格式字符窜
*/
@Test
public void testListToJson() {
List<DemoOne> demoOneList = getList();
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(demoOneList);
System.out.println("List<T> T为javabean 转 json格式字符窜");
System.out.println(jsonString);
}
/**
* json格式字符窜转换为java list<T> T为自定义实体类
*/
@Test
public void testJsonToList() {
String jsonString = "[{\"id\":1,\"userName\":\"userName--0\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--1\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--2\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--3\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0},{\"id\":1,\"userName\":\"userName--4\",\"bir\":\"2021-04-25 09:15:00\"," +
"\"salary\":10000.0}]\n";
Gson gson = getDateFormatGson();
List<DemoOne> demoOneList = gson.fromJson(jsonString, List.class);
System.out.println("json格式字符窜转为List<T> T为java bean");
System.out.println(demoOneList);
System.out.println("json格式字符窜转为List<T> T为java bean 其中索引为2的javabean的数据:");
System.out.println(demoOneList.get(2));
}
/**
* java bean中属性有java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledJavaBeanToJsonString() {
DemoTwo demoTwo = getJavaBeanFiledJavaBean();
System.out.println("javabean信息");
System.out.println(demoTwo);
System.out.println("转为json后的数据");
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(demoTwo);
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有java bean
*/
@Test
public void testJsonStringToJavaBeanFiledJavaBean() {
String jsonString = "{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 10:43:55\"," +
"\"salary\":10000.0,\"demo\":{\"id\":1,\"userName\":\"filedOfJavaBeanUserName\",\"bir\":\"2021-04-25 " +
"10:43:55\"}}\n";
System.out.println("json格式字符窜转为java对象的数据:");
Gson gson = getDateFormatGson();
DemoTwo demoTwo = gson.fromJson(jsonString, DemoTwo.class);
System.out.println(demoTwo);
System.out.println("json格式字符窜转为java对象中属性为javabean属性的数据:");
System.out.println(demoTwo.getDemo());
}
/**
* java bean中属性有List<T> t为自定义java bean ,转为json格式的字符窜
*/
@Test
public void testJavaBeanFiledListToJsonString() {
DemoThree demoThree = getJavaBeanFiledList();
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(demoThree);
System.out.println("javabean信息");
System.out.println(demoThree);
System.out.println("转为json后的数据");
System.out.println(jsonString);
}
/**
* json格式的字符窜 转为 java bean中属性有List<T> t为自定义java bean
*/
@Test
public void testJsonStringToJavaBeanFiledList() throws JsonProcessingException {
String jsonString = "{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 16:01:25\"," +
"\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 16:01:25\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 16:01:25\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 16:01:25\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 16:01:25\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 16:01:25\"}]}";
Gson gson = getDateFormatGson();
DemoThree demoThree = gson.fromJson(jsonString, DemoThree.class);
System.out.println("转换后的javabean信息");
System.out.println(demoThree);
System.out.println("转换后的javabean中list中信息");
System.out.println(demoThree.getDemoList());
System.out.println("转换后的javabean中list中索引为2的javabean信息");
System.out.println(demoThree.getDemoList().get(2));
}
/**
* list属性有 list 转为 json格式字符窜
*/
@Test
public void testlistFiledListToJsonString() {
List<DemoThree> listFiledList = getListFiledList();
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(listFiledList);
System.out.println("list信息");
System.out.println(listFiledList);
System.out.println("转为json后的数据");
System.out.println(jsonString);
}
/**
* json字符窜 转为 list 属性 中有list
* 报错:
* java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.demo.model
* .DemoThree
*/
@Test
public void testJsonStringTolistFiledList() {
String jsonString = "[{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 11:22:26\"," +
"\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]},{\"id\":1,\"userName\":\"javabeanOfNae\",\"bir\":\"2021-04-25 " +
"11:22:26\",\"salary\":10000.0,\"demoList\":[{\"id\":1,\"userName\":\"filedOfListName-----0\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----1\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----2\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----3\"," +
"\"bir\":\"2021-04-25 11:22:26\"},{\"id\":1,\"userName\":\"filedOfListName-----4\"," +
"\"bir\":\"2021-04-25 11:22:26\"}]}]\n";
Gson gson = getDateFormatGson();
/**
* 这种情况会报错,因为没有明确指定json字符窜转换后数据的类型
*/
// List<DemoThree> demoThreeList = gson.fromJson(jsonString, List.class);
/**
* 第一种方式指定json字符窜转换为java对象的数据类型
* 指定使用的是匿名内部类的方式
*/
// List<DemoThree> demoThreeList = gson.fromJson(jsonString, new TypeToken<List<DemoThree>>() {
// }.getType());
/**
* 第一种方式指定json字符窜转换为java对象的数据类型
* 指定使用的是匿名内部类的方式
*/
List<DemoThree> demoThreeList = gson.fromJson(jsonString, new MyTypeToken().getType());
System.out.println("转换后的list信息");
System.out.println(demoThreeList);
System.out.println("转换后的list索引为2信息");
System.out.println(demoThreeList.get(2));
System.out.println("转换后的list索引为2属性中list中信息");
System.out.println(demoThreeList.get(2).getDemoList());
System.out.println("转换后的list索引为2属性中list索引为2信息");
System.out.println(demoThreeList.get(2).getDemoList().get(2));
}
/**
* map 转换为 json格式字符窜
*/
@Test
public void testMapToJsonString() {
Map<String, Object> map = getMap();
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(map);
System.out.println("map信息");
System.out.println(map);
System.out.println("map转为json格式后:");
System.out.println(jsonString);
}
/**
* json 格式转map
*/
@Test
public void testJsonStringToMap() {
String jsonString = "{\"bir\":1619328801393,\"id\":1,\"userName\":\"张三\",\"salary\":10000.0}";
// String jsonString = "{bir=Sun Apr 25 16:14:45 CST 2021, id=1, userName=张三, salary=10000.0}";
Gson gson = getDateFormatGson();
Map map = gson.fromJson(jsonString, Map.class);
System.out.println("json格式字符串转map对象");
System.out.println(map);
}
/**
* list<map> 转为 json格式的字符窜
*/
@Test
public void testListMapToJsonString() {
List<Map<String, Object>> listMap = getListMap();
Gson gson = getDateFormatGson();
String jsonString = gson.toJson(listMap);
System.out.println("list<map>的数据:");
System.out.println(listMap);
System.out.println("list<map>转为json字符窜的数据:");
System.out.println(jsonString);
}
/**
* json格式字符窜 转为 list<map>
*/
@Test
public void testJsonStringToListMap() {
String jsonString = "[{\"bir\":1619329384863,\"id\":1,\"userName\":\"zhangsan--0\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--1\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--2\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--3\",\"salary\":10000.0}," +
"{\"bir\":1619329384902,\"id\":1,\"userName\":\"zhangsan--4\",\"salary\":10000.0}]\n";
Gson gson = getDateFormatGson();
List listMap = gson.fromJson(jsonString, List.class);
System.out.println("json格式字符窜转为list<map>");
System.out.println(listMap);
System.out.println("获取list集合第一个元素的map,再map中key为id的value的值");
Map map = (Map) listMap.get(1);
System.out.println(map);
Object id = map.get("id");
System.out.println(id);
}
}
需要辅助的工具类
import com.example.demo.model.DemoThree;
import com.google.gson.reflect.TypeToken;
import java.util.Collection;
import java.util.List;
/**
* @author shaoming
* @Date: 2021/4/25 16:07
* @Description:
* Gson中定义的接口
*用于指定json字符窜转为java对象的类型
* 不使用这个接口报错:
*
* java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.demo.model
*/
public class MyTypeToken extends TypeToken<List<DemoThree>> {
}