-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList_conversion_json.java
More file actions
50 lines (40 loc) · 1.25 KB
/
List_conversion_json.java
File metadata and controls
50 lines (40 loc) · 1.25 KB
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
package json.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import json.domain.Person;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class List_conversion_json {
public static void main(String[] args) throws Exception{
//1.创建Person对象
Person p = new Person();
p.setName("cpu");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
Person p1 = new Person();
p1.setName("cpu_code");
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);
System.out.println(json);
}
}
/*
[{"name":"cpu","age":23,"gender":"男","birthday":"2020-12-02"},
{"name":"cpu_code","age":23,"gender":"男","birthday":"2020-12-02"},
{"name":"哈哈哈","age":23,"gender":"男","birthday":"2020-12-02"}]
* */