-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathJsonToList.java
More file actions
100 lines (74 loc) · 2.14 KB
/
JsonToList.java
File metadata and controls
100 lines (74 loc) · 2.14 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package serialization;
import com.alibaba.fastjson.JSON;
import serialization.jackson.JacksonUtils;
import java.util.Arrays;
import java.util.List;
/**
* @author vonzhou
* @date 2018/12/6
*/
public class JsonToList {
public static void main(String[] args) {
String s = "[{\"name\":\"vonz\", \"age\":99}]";
List<Foo> list = useJackson(s);
System.out.println(list);
list = useFastjson(s);
System.out.println(list);
s = "[{\"name\":\"vonz\", \"age\":99}, {\"name\":\"vonz\", \"age\":99}]";
list = useJacksonMultiType(s);
System.out.println(list);
list = useFastjson(s);
System.out.println(list);
}
public static List<Foo> useJackson(String s) {
Foo[] ar = JacksonUtils.fromJson(s, Foo[].class);
return Arrays.asList(ar);
}
public static List<Foo> useJacksonMultiType(String s) {
Foo[] ar = JacksonUtils.fromJson(s, Foo[].class);
return Arrays.asList(ar);
}
public static List<Foo> useFastjson(String s) {
return JSON.parseArray(s, Foo.class);
}
static class Foo {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Foo{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
static class Bar extends Foo {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Bar{" +
"name='" + super.name + '\'' +
", age=" + super.age +
", city='" + city + '\'' +
'}';
}
}
}