Jackson是Java语言处理数据的一个工具集,包括一流的JSON解析功能,另外还有模块支持Avro
,CBOR
,CSV
,Smile
,XML
或者YAML
格式数据的处理,所支持的数据格式还在不断增加。Jackson 2.x版本官方主页https://github.com/FasterXML/jackson。
目前比较流行的json解析工具类库有Jackson,JSON-lib,Gson,在性能方面Jackson>Gson>Json-lib.
测试结果参考:
1 class Person {
2 String name;
3 int age;
4 }
会报异常,因为没有getter,setter方法,之后分别测试有无@JsonProperty(“Three”)的情况
1 class Person {
2 String name;
3 String FirstName;
4 public firstName;
5 String LastName;
6 public lastName;
7 public IDCard;
8 }
有public声明的成员变量,没有getter,setter方法也不会报异常
1 static class test {
2 String one;
3 String two;
4 @JsonProperty("Three")
5 public String Three;
6 public String FOur;
7 public String FOIuErParties;
8
9 public String getThree() {
10 return Three;
11 }
12
13 public void setThree(String three) {
14 Three = three;
15 }
16
17 public String getFOIuErParties() {
18 return FOIuErParties;
19 }
20
21 public void setFOIuErParties(String fOIuErParties) {
22 FOIuErParties = fOIuErParties;
23 }
24
25 public String getOne() {
26 return one;
27 }
28
29 public void setOne(String one) {
30 this.one = one;
31 }
32
33 public String getTwo() {
34 return two;
35 }
36
37 public void setTwo(String two) {
38 this.two = two;
39 }
40
41 /*
42 public String getThree() {
43 return Three;
44 }
45
46 public void setThree(String three) {
47 Three = three;
48 }
49 */
50 test(String one,String two, String Three) {
51 this.one = one;
52 this.two = two;
53 this.Three = Three;
54 }
55 }
可以使用PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy
类来定义Java对象成员变量名转换成json字段名的转换规则。
使用起来很简单,代码如下:
1 ObjectMapper mapper = new ObjectMapper();
2 mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy());
3 try {
4 json = mapper.writeValueAsString(formDataTpls);
5 //增加total,success,msg,code字段
6
7 } catch(JsonProcessingException e) {
8 e.printStackTrace();
9 }
10
11
12 ObjectMapper mapper = new ObjectMapper();
13 //mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy());
14 mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); // but only public getters
15 //mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); JsonMethod枚举值已经废弃,改用PropertyAccessor
16 //mapper.setVisibilityChecker(mapper.getVisibilityChecker().with(JsonAutoDetect.Visibility.NONE));
17 try {
18 json = mapper.writeValueAsString(formDataTpls);
19 //增加total,success,msg,code字段
20
21 } catch(JsonProcessingException e) {
22 e.printStackTrace();
23 }
24 return json;
25
26
27 User user = new User();
28 //...set user data
29
30 ObjectMapper mapper = new ObjectMapper();
31 System.out.println(mapper.writeValueAsString(user));
output
{“age”:29,”messages”:[“msg 1”,”msg 2”,”msg 3”],”name”:”mkyong”}
ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
Output
{ “age” : 29, “messages” : [ “msg 1”, “msg 2”, “msg 3” ], “name” : “mkyong” }
//mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy()); 下划线格式命名