`

Jackson 高性能的JSON处理 ObjectMapper

阅读更多

转文链接:http://blog.csdn.net/wangyang2698341/article/details/8223929

 

 今天自行研究了下json ,感觉非常好用,经过测试比google的GSON快多了

      同时Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。功能非常的强悍!

       大家也知道,json 在如今互联网时代应用的非常广,因为大家如此的关注,所以对json的解析性能要求也是非常高的。

 

一、 准备工作

1、 下载依赖库jar包

Jackson的jar all下载地址:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar

然后在工程中导入这个jar包即可开始工作

官方示例:http://wiki.fasterxml.com/JacksonInFiveMinutes

因为下面的程序是用junit测试用例运行的,所以还得添加junit的jar包。版本是junit-4.2.8

如果你需要转换xml,那么还需要stax2-api.jar

2、 测试类基本代码如下

 

  1. /* 
  2.  * @project java 
  3.  * @package  
  4.  * @file Jackson.java 
  5.  * @version  1.0 
  6.  * @author   廖益平 
  7.  * @time  2011-11-8 上午02:59:37 
  8.  */  
  9.   
  10. public class Jackson {  
  11.     /* 
  12.      * 
  13.      * Class Descripton goes here. 
  14.      * 
  15.      * @class Jackson 
  16.      * @version  1.0 
  17.      * @author   廖益平 
  18.      * @time  2011-11-8 上午02:59:37 
  19.      */  
  20.     public  static JsonGenerator jsonGenerator = null;  
  21.     private static ObjectMapper mapper = new ObjectMapper();  
  22.     public static void main(String[] args) {  
  23.         Student student = new Student();  
  24.         student.setIsstudent(true);  
  25.         student.setUid(1000);  
  26.         student.setUname("xiao liao");  
  27.         student.setUpwd("123");  
  28.         student.setNumber(12);  
  29.           
  30.         Map<String, Student> stuMap = new HashMap<String, Student>();  
  31.         stuMap.put("1", student);  
  32.         stuMap.put("2", student);  
  33.           
  34.         List<Object> stuList = new ArrayList<Object>();  
  35.         List<Student> stuList1 = new ArrayList<Student>();  
  36.         stuList1.add(student);  
  37.         student=  new  Student();  
  38.         student.setIsstudent(false);  
  39.         student.setUid(200);  
  40.         student.setUname("xiao mi");  
  41.         stuList1.add(student);  
  42.           
  43.         stuList.add(student);  
  44.         stuList.add("xiao xin");  
  45.         stuList.add("xiao er");  
  46.         stuList.add(stuMap);  
  47.           
  48.         //readJson2List();  
  49.         try {  
  50.             //readJson2Array();  
  51.             //writeArray2Json(array);  
  52.             //writeJson2List();  
  53.             //writeEntity2Json(student);  
  54.             writeJson2Entity();  
  55.             //writeMap2Json(stuMap);  
  56.             //writeList2Json(stuList1);  
  57.               
  58.         } catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  
  62.      /** 
  63.       *  
  64.       * <code>writeEntity2Json</code> 
  65.       * @description: TODO(实体类转换成json)  
  66.       * @param object 
  67.       * @throws IOException 
  68.       * @since   2011-11-8     廖益平 
  69.       */  
  70.      public static void writeEntity2Json(Object object) throws IOException {  
  71.            mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );  
  72.            mapper.writeValue( System.out,object );  
  73.            
  74.      }  
  75.      /** 
  76.       *  
  77.       * <code>writeArray2Json</code> 
  78.       * @description: TODO(数组转换成json数组)  
  79.       * @param object 
  80.       * @throws IOException 
  81.       * @since   2011-11-8     廖益平 
  82.       */  
  83.      public static void writeArray2Json(Object object) throws IOException {  
  84.            
  85.          // writeValue具有和writeObject相同的功能  
  86.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );  
  87.          mapper.writeValue(System.out,object );  
  88.            
  89.      }  
  90.      /** 
  91.       *  
  92.       * <code>writeMap2Json</code> 
  93.       * @description: TODO(map对象转换成json对象)  
  94.       * @param object 
  95.       * @throws IOException 
  96.       * @since   2011-11-8     廖益平 
  97.       */  
  98.      public static void writeMap2Json(Object object) throws IOException {  
  99.            
  100.          System.out.println("使用ObjectMapper-----------");  
  101.          // writeValue具有和writeObject相同的功能  
  102.          System.out.println("==>"+mapper.writeValueAsString(object));  
  103.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );  
  104.          mapper.writeValue( System.out , object );  
  105.      }  
  106.      /** 
  107.       *  
  108.       * <code>writeList2Json</code> 
  109.       * @description: TODO(list转换成json)  
  110.       * @param object 
  111.       * @throws IOException 
  112.       * @since   2011-11-8     廖益平 
  113.       */  
  114.      public static void writeList2Json(Object object) throws IOException {  
  115.          System.out.println("==>"+mapper.writeValueAsString(object));  
  116.          mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );  
  117.          mapper.writeValue( System.out , object );  
  118.      }  
  119.      /** 
  120.       *  
  121.       * <code>writeJson2Entity</code> 
  122.       * @description: TODO(json转换成实体)  
  123.       * @throws IOException 
  124.       * @since   2011-11-8     廖益平 
  125.       */  
  126.      public static void writeJson2Entity() throws IOException {  
  127.          System.out.println("json串转换成entity-------------");  
  128. //       File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");  
  129. //       FileInputStream inputStream = new FileInputStream(file);  
  130. //       Student student = mapper.readValue(inputStream,Student.class);  
  131. //       System.out.println(student.toString());  
  132.          //漂亮输出  
  133.          //mapper.defaultPrettyPrintingWriter().writeValueAsString(value);  
  134.       
  135.          String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}";  
  136.          Student student1 = mapper.readValue(json,Student.class);  
  137.          System.out.println("json2:"+student1.toString());  
  138.      }  
  139.      /** 
  140.       *  
  141.       * <code>writeJson2List</code> 
  142.       * @description: TODO(json专程list对象)  
  143.       * @throws IOException 
  144.       * @since   2011-11-8     廖益平 
  145.       */  
  146.      public static void writeJson2List() throws IOException {  
  147.          System.out.println("json串转换成entity-------------");  
  148.          File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");  
  149.          FileInputStream inputStream = new FileInputStream(file);  
  150.          Student student = mapper.readValue(inputStream,Student.class);  
  151.          System.out.println(student.toString());  
  152.            
  153.          String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";  
  154.          List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class);  
  155.          for (int i = 0; i < s.size(); i++) {  
  156.              LinkedHashMap<String, Object> link = s.get(i);  
  157.              Set<String>  key = link.keySet();  
  158.              for (Iterator iterator = key.iterator(); iterator.hasNext();) {  
  159.                 String string = (String) iterator.next();  
  160.                 System.out.println(string+"==>"+link.get(string));  
  161.                   
  162.             }  
  163.              System.out.println("json:"+i+""+s.get(i).toString());  
  164.               
  165.         }  
  166.      }  
  167.      /** 
  168.        * JSON转换为List对象 
  169.        */  
  170.       public static void readJson2List() {  
  171.        String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"  
  172.          + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";  
  173.        try {  
  174.         List<LinkedHashMap<String, Object>> list = mapper.readValue(  
  175.           json, List.class);  
  176.         System.out.println(list.size());  
  177.         for (int i = 0; i < list.size(); i++) {  
  178.          Map<String, Object> map = list.get(i);  
  179.          Set<String> set = map.keySet();  
  180.          for (Iterator<String> it = set.iterator(); it.hasNext();) {  
  181.           String key = it.next();  
  182.           System.out.println(key + ":" + map.get(key));  
  183.          }  
  184.         }  
  185.        } catch (JsonParseException e) {  
  186.         e.printStackTrace();  
  187.        } catch (JsonMappingException e) {  
  188.         e.printStackTrace();  
  189.        } catch (IOException e) {  
  190.         e.printStackTrace();  
  191.        }  
  192.       }  
  193.       /** 
  194.        * JSON转换为List对象 
  195.        */  
  196.       public static void readJson2Array() {  
  197.           String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"  
  198.               + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";  
  199.           try {  
  200.               Student[] students = mapper.readValue(json, Student[].class);  
  201.               for (Student student : students) {  
  202.                 System.out.println(">"+student.toString());  
  203.             }  
  204.           } catch (JsonParseException e) {  
  205.               e.printStackTrace();  
  206.           } catch (JsonMappingException e) {  
  207.               e.printStackTrace();  
  208.           } catch (IOException e) {  
  209.               e.printStackTrace();  
  210.           }  
  211.       }  
  212.   
  213. }  

打印结果 :

串转换成entity-------------
json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isStudent=true

writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

readJson2Array------------------
>uid=1,name=www,upwd=456,number=234.0,isStudent=false
>uid=5,name=tom,upwd=123,number=3.44,isStudent=false
writeMap2Json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

大家逐个自己试试吧  ,上面也是我的测试代码

 

  1. 实体类:  
  2. /* 
  3.  * @project java 
  4.  * @package  
  5.  * @file Student.java 
  6.  * @version  1.0 
  7.  * @author   廖益平 
  8.  * @time  2011-11-8 上午03:01:08 
  9.  */  
  10.   
  11. public class Student {  
  12.     /* 
  13.      * 
  14.      * Class Descripton goes here. 
  15.      * 
  16.      * @class Student 
  17.      * @version  1.0 
  18.      * @author   廖益平 
  19.      * @time  2011-11-8 上午03:01:08 
  20.      */  
  21.       private int uid;  
  22.       private String uname;  
  23.       private String upwd;  
  24.       private double number;  
  25.       private boolean isstudent;  
  26.     public int getUid() {  
  27.         return uid;  
  28.     }  
  29.     public void setUid(int uid) {  
  30.         this.uid = uid;  
  31.     }  
  32.     public String getUname() {  
  33.         return uname;  
  34.     }  
  35.     public void setUname(String uname) {  
  36.         this.uname = uname;  
  37.     }  
  38.     public String getUpwd() {  
  39.         return upwd;  
  40.     }  
  41.     public void setUpwd(String upwd) {  
  42.         this.upwd = upwd;  
  43.     }  
  44.     public double getNumber() {  
  45.         return number;  
  46.     }  
  47.     public void setNumber(double number) {  
  48.         this.number = number;  
  49.     }  
  50.     public boolean isIsstudent() {  
  51.         return isstudent;  
  52.     }  
  53.     public void setIsstudent(boolean isstudent) {  
  54.         this.isstudent = isstudent;  
  55.     }  
  56.     @Override  
  57.     public String toString() {  
  58.       
  59.         return "uid="+uid+",name="+uname+",upwd="+upwd+",number="+number+",isStudent="+isstudent;  
  60.     }  
  61.       
  62.         
  63. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics