1. Json
1.1. 简介
1.1.1. 为什么学习jackson框架
json格式在web开发应用中非常广泛,格式非常简单。我们可以借助json的疼,进行跨平台的数据传输。而jackson框架可以用来处理json相关的数据,能够做到以下事情:
- 把java端的对象转换成json格式字符串
- 将json格式的字符串转换成java对象
而我们选择jackson的原因:
- spring框架对应json格式解析的时候,优先选择jackson
1.1.2. 常见的json框架
- fastjson => alibaba
- Gson => google
1.2.3. jackson框架处理的是什么样的数据
处理json格式的数据。可以做到:
- 把对象转换成json字符串
- 把json字符串转换为对象
1.2. 对象转json字符串
1.2.1. 环境准备
- 新建maven项目,在pom.xml引入jackson的依赖
<!--jackson依赖,版本应该大于2.7.0-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.3</version>
</dependency>
<!--lombok和junit单元测试-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- 创建测试用的pojo对象
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer sid;
private String gender;
private String birthday;
private List<String> hobbies;
}
1.2.2. 对象转json字符串
对象会直接转换为一个json对象的字符串
@Test
public void testObject2Json() throws JsonProcessingException {
ArrayList<String> hobbies = new ArrayList<>();
hobbies.add("唱");
hobbies.add("跳");
hobbies.add("rap");
hobbies.add("篮球");
Student student = Student.builder().sid(001).birthday("1999-01-01").gender("1").hobbies(hobbies).build();
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(student);
System.out.println(jsonStr);
}
1.2.3. 对象集合转json字符串
对象集合会转换为json数组的字符串
@Test
public void testList2Json() throws JsonProcessingException {
ArrayList<String> hobbies = new ArrayList<>();
hobbies.add("唱");
hobbies.add("跳");
hobbies.add("rap");
hobbies.add("篮球");
Student student = Student.builder().sid(001).birthday("1999-01-01").gender("1").hobbies(hobbies).build();
ArrayList<Student> stuList = new ArrayList<>();
stuList.add(student);
stuList.add(student);
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(stuList);
System.out.println(jsonStr);
}
1.2.4.Map转字符串
Map会直接转换为一个json对象的字符串
@Test
public void testMap2Json() throws JsonProcessingException {
ArrayList<String> hobbies = new ArrayList<>();
hobbies.add("唱");
hobbies.add("跳");
hobbies.add("rap");
hobbies.add("篮球");
HashMap<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("id","002");
map.put("gender",1);
map.put("hobby",hobbies);
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(map);
System.out.println(jsonStr);
}
1.3. json字符串转对象
json字符串转单个对象
@Test
public void testReadObject() throws JsonProcessingException {
String jsonStr = "{\"sid\":1,\"gender\":\"1\",\"birthday\":\"1999-01-01\",\"hobbies\":[\"唱\",\"跳\",\"rap\",\"篮球\"]}";
ObjectMapper objectMapper = new ObjectMapper();
Student student = objectMapper.readValue(jsonStr, Student.class);
System.out.println(student);
}
json数组转对象集合
在使用readValue()转换成集合的时候,应该使用TypeReference的匿名内部类的泛型指定返回值类型
@Test
public void testReadList() throws JsonProcessingException {
String jsonStr = "[{\"sid\":1,\"gender\":\"1\",\"birthday\":\"1999-01-01\",\"hobbies\":[\"唱\",\"跳\",\"rap\",\"篮球\"]}," +
"{\"sid\":1,\"gender\":\"1\",\"birthday\":\"1999-01-01\",\"hobbies\":[\"唱\",\"跳\",\"rap\",\"篮球\"]}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Student> stuList = objectMapper.readValue(jsonStr, new TypeReference<List<Student>>() {});
stuList.forEach(System.out::println);
}
Comments NOTHING