JSON转换工具fastjson和jackson的总结

在本帖子中,我们来一起总结一下fastjson和jackson的常用API。
首先准备一个实体类User:
[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class User {
        private Integer id;
        private String name;
        private Date birthday;
         
        public Integer getId() {
                return id;
        }
        public void setId(Integer id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
//        @JSONField(format="yyyy-mm-dd")
        public Date getBirthday() {
                return birthday;
        }
        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
        @Override
        public String toString() {
                SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
                return "User [id=" + id + ", name=" + name + ", birthday=" + dataFormat.format(birthday) + "]";
        }
}


 

接下来,开始我们的测试,下面的测试代码中已经将测试要求和结果进行了打印
[Java] 纯文本查看 复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
public class JsonTest {
     
    //准备存放了一个对象的json字符串
    private String userStr = "{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"}";
    //准备存放了一个集合的json字符串
    private String userArr = "[{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"},"
            + "{\"id\":2,\"name\":\"顾里\",\"birthday\":\"2012-08-01\"}]";
     
    private User user;
     
    private List<User> users = new ArrayList<User>();
     
    @Before
    public void initUser() {
        //初始化一个用户
        User user = new User();
        user.setId(1);
        user.setName("林萧");
        user.setBirthday(new Date());
        this.user = user;
         
        //初始化一个集合
        User user1 = new User();
        user1.setId(1);
        user1.setName("林萧");
        user1.setBirthday(new Date());
        users.add(user1);
         
        User user2 = new User();
        user2.setId(2);
        user2.setName("顾源");
        user2.setBirthday(new Date());
        users.add(user2);
    }
 
    //fastjson
    @Test
    public void fastjson_json2Bean() {
        //[1-1] 对象格式json串转对象
        User user = JSONObject.parseObject(userStr, User.class);
        System.out.println(user);
        //打印效果如下:
        //User [id=1, name=简溪, birthday=2010-01-01]
         
        //[1-2] 集合格式json串转对象
        List<User> users = JSONArray.parseArray(userArr, User.class);
        System.out.println(users);
        //打印效果如下:
//      [User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
    }
     
    @Test
    public void fastjson_bean2Json() {
        //[1-3] 对象转json串
        String userJson = JSONObject.toJSONString(user);
        System.out.println(userJson);
        //打印效果如下:
//      {"birthday":1530716739741,"id":1,"name":"林萧"}
         
        //[1-4] 集合转json串
        String usersStr = JSONArray.toJSONString(users);
        System.out.println(usersStr);
        //打印效果如下:
//      [{"birthday":1530716739741,"id":1,"name":"林萧"},{"birthday":1530716739741,"id":2,"name":"顾源"}]
    }
     
    //jackson
    @Test
    public void jackson_json2Bean() throws Exception {
        //[2-1] 对象格式json串转对象
        ObjectMapper jackson = new ObjectMapper();
        User user = jackson.readValue(userStr, User.class);
        System.out.println(user);
        //打印结果
//      User [id=1, name=简溪, birthday=2010-01-01]
         
        //[2-2 对象格式json串转对象
        List<User> users = jackson.readValue(userArr, new TypeReference<List<User>>() {});
        System.out.println(users);
        //打印结果
//      [User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
在本帖子中,我们来一起总结一下fastjson和jackson的常用API。
首先准备一个实体类User:
[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class User {
        private Integer id;
        private String name;
        private Date birthday;
         
        public Integer getId() {
                return id;
        }
        public void setId(Integer id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
//        @JSONField(format="yyyy-mm-dd")
        public Date getBirthday() {
                return birthday;
        }
        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
        @Override
        public String toString() {
                SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
                return "User [id=" + id + ", name=" + name + ", birthday=" + dataFormat.format(birthday) + "]";
        }
}

接下来,开始我们的测试,下面的测试代码中已经将测试要求和结果进行了打印
[Java] 纯文本查看 复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
public class JsonTest {
     
    //准备存放了一个对象的json字符串
    private String userStr = "{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"}";
    //准备存放了一个集合的json字符串
    private String userArr = "[{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"},"
            + "{\"id\":2,\"name\":\"顾里\",\"birthday\":\"2012-08-01\"}]";
     
    private User user;
     
    private List<User> users = new ArrayList<User>();
     
    @Before
    public void initUser() {
        //初始化一个用户
        User user = new User();
        user.setId(1);
        user.setName("林萧");
        user.setBirthday(new Date());
        this.user = user;
         
        //初始化一个集合
        User user1 = new User();
        user1.setId(1);
        user1.setName("林萧");
        user1.setBirthday(new Date());
        users.add(user1);
         
        User user2 = new User();
        user2.setId(2);
        user2.setName("顾源");
        user2.setBirthday(new Date());
        users.add(user2);
    }
 
    //fastjson
    @Test
    public void fastjson_json2Bean() {
        //[1-1] 对象格式json串转对象
        User user = JSONObject.parseObject(userStr, User.class);
        System.out.println(user);
        //打印效果如下:
        //User [id=1, name=简溪, birthday=2010-01-01]
         
        //[1-2] 集合格式json串转对象
        List<User> users = JSONArray.parseArray(userArr, User.class);
        System.out.println(users);
        //打印效果如下:
//      [User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
    }
     
    @Test
    public void fastjson_bean2Json() {
        //[1-3] 对象转json串
        String userJson = JSONObject.toJSONString(user);
        System.out.println(userJson);
        //打印效果如下:
//      {"birthday":1530716739741,"id":1,"name":"林萧"}
         
        //[1-4] 集合转json串
        String usersStr = JSONArray.toJSONString(users);
        System.out.println(usersStr);
        //打印效果如下:
//      [{"birthday":1530716739741,"id":1,"name":"林萧"},{"birthday":1530716739741,"id":2,"name":"顾源"}]
    }
     
    //jackson
    @Test
    public void jackson_json2Bean() throws Exception {
        //[2-1] 对象格式json串转对象
        ObjectMapper jackson = new ObjectMapper();
        User user = jackson.readValue(userStr, User.class);
        System.out.println(user);
        //打印结果
//      User [id=1, name=简溪, birthday=2010-01-01]
         
        //[2-2 对象格式json串转对象
        List<User> users = jackson.readValue(userArr, new TypeReference<List<User>>() {});
        System.out.println(users);
        //打印结果
//      [User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
    }
     
    @Test
    public void jackson_bean2Json() throws Exception {
         
        ObjectMapper jackson = new ObjectMapper();
        //[2-3] 对象转json串
        String userJson = jackson.writeValueAsString(user);
        System.out.println(userJson);
        //打印结果
//      {"id":1,"name":"林萧","birthday":1530717373507}
         
        //[2-4] 集合转json串
        String usersJson = jackson.writeValueAsString(users);
        System.out.println(usersJson);
        //打印结果
//      [{"id":1,"name":"林萧","birthday":1530717401460},{"id":2,"name":"顾源","birthday":1530717401460}]
         
    }
}

本次总结中,比较难的是jackson中将json字符串转成对象集合的过程。
PS:详细代码参见附件——本项目基于maven。
    }
     
    @Test
    public void jackson_bean2Json() throws Exception {
         
        ObjectMapper jackson = new ObjectMapper();
        //[2-3] 对象转json串
        String userJson = jackson.writeValueAsString(user);
        System.out.println(userJson);
        //打印结果
//      {"id":1,"name":"林萧","birthday":1530717373507}
         
        //[2-4] 集合转json串
        String usersJson = jackson.writeValueAsString(users);
        System.out.println(usersJson);
        //打印结果
//      [{"id":1,"name":"林萧","birthday":1530717401460},{"id":2,"name":"顾源","birthday":1530717401460}]
         
    }
}

本次总结中,比较难的是jackson中将json字符串转成对象集合的过程。
PS:详细代码参见附件——本项目基于maven。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值