Ultra-simple jackson Data Binding po, pojo, vo, will be used in 10 minutes (original)

Source: Internet
Author: User

The project needs to use json to operate data, search for several evaluations on the internet, add your own Visual Test (-_-#), and finally decide to use jackson, and then start searching materials for learning.

I want to convert objects to json characters, convert characters to objects, and parse POJO and VO.
At the beginning, I felt quite depressed. There was not much information, so I had to look at the source code in English. it took only half a day to know that jackson was quite powerful, but it was quite simple to use. all the functions we need are taken into account by the author.

I wrote some test code, just a few lines, but I have all the requirements I need. First, I got the code:

Ro class

1 package;
2 /**
3 * automatically generated vo, corresponding to the database tb_Role table
4 */
5 public class RoleVO implements java. io. Serializable {
6 private int roleId;
7 private String roleName;
8 private int lv;
9
10 public void setRoleId (int value ){
11 this. roleId = value;
12}
13
14 public int getRoleId (){
15 return roleId;
16}
17
18 public void setRoleName (String value ){
19 this. roleName = value;
20}
21
22 public String getRoleName (){
23 return roleName;
24}
25
26 public void setLv (int value ){
27 this. lv = value;
28}
29
30 public int getLv (){
31 return lv;
32}
33}

 

1 public static void main (String args []) {
2 try {
3 // convert the json string to vo/po/pojo object
4 String strRo = "{\" roleId \ ": 1, \" roleName \ ": \" role1 \ ", \" lv \ ": 0 }";
5 RoleVO role = mapper. readValue (strRo, RoleVO. class );
6
7 // convert the json string to List (including the generic type in List)
8 String strLs = "[{\" roleId \ ": 1, \" roleName \ ": \" role1 \ ", \" lv \ ": 0}," +
9 "{\" roleId \ ": 2, \" roleName \ ": \" role2 \ ", \" lv \ ": 2}," +
10 "{\" roleId \ ": 3, \" roleName \ ": \" role3 \ ", \" lv \ ": 0}]";
11 List <RoleVO> roleLs = mapper. readValue (strLs, new TypeReference <List <RoleVO> (){});
12 System. out. println ("json to list type" + roleLs );
13
14 // convert the json string to Map (including the generic type in Map)
15 String strMap = "{\" role1 \ ": {\" roleId \ ": 1, \" roleName \ ": \" role1 \ ", \" lv \": 0}, "+
16 "\" role2 \ ": {\" roleId \ ": 1, \" roleName \ ": \" role2 \ ", \" lv \ ": 0}," +
17 "\" role3 \ ": {\" roleId \ ": 1, \" roleName \ ": \" role3 \ ", \" lv \ ": 0 }}";
18 Map <String, RoleVO> roleMap = mapper. readValue (strMap, new TypeReference <Map <String, RoleVO >> (){});
19 System. out. println (roleMap );
20
21 // convert an object to a json string
22 String mapStr = mapper. writeValueAsString (roleMap );
23 String lsStr = mapper. writeValueAsString (roleLs );
24 String voStr = mapper. writeValueAsString (role );
25
26 System. out. println (mapStr );
27 System. out. println (lsStr );
28 System. out. println (voStr );
29} catch (Exception e ){
30 System. out. println ("Conversion error ");
31}
32}

The operation is quite simple, but the function is very powerful. The TypeReference class is very clever. The original java generic type can also be used in this way. The detailed information should be searched by google.

If you want to cache, jackson also provides the cache interface and can perform io operations. However, I don't need it, so I haven't studied it yet.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.