轻量级数据交换格式-JSON解析器

JSON解析器

一些封装好的工具

常见的解析器:Jsonlib,Gson,fastjson,jackson(今天学习的)

JSON转换为Java对象

使用步骤

  1. 导入Jackson的相关jar包
  2. 创建Jackson核心对象ObjectMapper
  3. 调用ObjectMapper的相关方法进行转换
  • 转换方法:

    readValue (json字符串数据,class)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//演示: JSON字符串转换为Java对象


@Test
public void test5() throws Exception {

//1.初始化JSON字符串
String json = "{\"name\":\"张三\",\"age\":23,\"gender\":\"男\",\"birthday\":\"2020-09-11\"}";

//2. 创建ObjectMapper对象
ObjectMapper mapper = new ObjectMapper();

//3. 转换为Java对象 Pertson对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);

}

Java对象转换为JSON

使用步骤

  1. 导入Jackson的相关jar包
  2. 创建Jackson核心对象ObjectMapper
  3. 调用ObjectMapper的相关方法进行转换
  • 转换方法:

    writeValue(参数1,obj)
        参数1:
         File:将obj对象转换为JSON字符串,并保存到指定的文件中
       writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
        OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
    
    writeValueAsString(obj):
            将对象转换为json字符串
  • 注解

    1. @JsonIgnore:排除属性
    2. @JsonFormat :属性值的格式化
      (使用时都是加在私有属性上方)

代码

不包含person实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.web.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.domain.Person;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.util.*;

public class JacksonTest {
//Java 对象转为JSON字符串
@Test
public void test1() throws Exception {
//创建person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");

//2.创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();


//3.转换

/*
* 转换方法:
* writeValue(参数1,obj)
* 参数1:
* File:将obj对象转换为JSON字符串,并保存到指定的文件中
* writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
* OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
* writeValueAsString(obj):将对象转换为json字符串
*
* */

String json = mapper.writeValueAsString(p);

//{"name":"张三","age":23,"gender":"男"}
System.out.println(json);

//writeValue.将数据写到中F:/JavaCode/JavaEE/7-JSON/src/a.txt文件中
mapper.writeValue(new File("F:/JavaCode/JavaEE/7-JSON/src/a.txt"),p);

//writeValue.将数据关联到writer中
mapper.writeValue(new FileWriter("F:/JavaCode/JavaEE/7-JSON/src/b.txt"),p);
}



@Test
public void test2() throws Exception {
//创建person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());


//2.转换

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(p);
System.out.println(json);



}
}

运行结果:

test1:
test2:

复杂Java对象转换

类型

  1. List集合:数组
  2. Map集合 :对象格式一致

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@Test
public void test3() throws Exception {
//创建person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());

Person p1 = new Person();
p1.setName("张三");
p1.setAge(23);
p1.setGender("男");
p1.setBirthday(new Date());

Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());

//创建List集合
List<Person> ps = new ArrayList<Person>();
ps.add(p);
ps.add(p1);
ps.add(p2);

//2.转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(ps);

//[{"name":"张三","age":23,"gender":"男","birthday":"2020-09-11"},{"name":"张三","age":23,"gender":"男","birthday":"2020-09-11"},{"name":"张三","age":23,"gender":"男","birthday":"2020-09-11"}]
System.out.println(json);
}



@Test
public void test4() throws Exception {
//创建map对象
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","李四");
map.put("age",23);
map.put("gender","男");

//2.转换

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);

System.out.println(json);



}

test3:


test4:

文章目录
  1. 1. JSON解析器
  2. 2. JSON转换为Java对象
    1. 2.1. 使用步骤
  3. 3. Java对象转换为JSON
    1. 3.1. 使用步骤
    2. 3.2. 代码
    3. 3.3. 复杂Java对象转换
      1. 3.3.1. 类型
      2. 3.3.2. 代码
,