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

Jackson 高性能的JSON处理 ObjectMapper的更多相关文章

  1. Jackson轻易转换JSON

    原文http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html Jackson可以轻松的将Java对象转换成json对象和xml文档,同样 ...

  2. jackson实体转json时 为NULL不参加序列化的汇总

    首先加入依赖 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson ...

  3. 使用jackson美化输出json/xml

    转载:http://www.cnblogs.com/xiwang/ 如何使用jackson美化输出json/xml 1.美化POJO序列化xml 下面将POJO列化为xml并打印. Person pe ...

  4. 如何使用jackson美化输出json/xml

    如何使用jackson美化输出json/xml 1.美化POJO序列化xml 下面将POJO列化为xml并打印. Person person = new Person(); //设置person属性 ...

  5. 用 Jackson 来处理 JSON

    Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好. 首先创建一个User对象类 (User.java) package com.sivalabs.json; impor ...

  6. Jackson 对象与json数据互转工具类JacksonUtil

    1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...

  7. (六)利用JackSon工具将JSON文件和对象互转

    1. 需要下载JackSon工具,并导入到: 2. 编写html页面: <!DOCTYPE html> <html> <head> <meta charset ...

  8. jackson解析处理JSON

    package com.ruoyi.common.json; import java.io.File; import java.io.IOException; import java.io.Input ...

  9. 用jackson包实现json、对象、Map之间的转换

    jackson API的使用 用jackson包实现json.对象.Map之间的转换

随机推荐

  1. Android的IPC机制(一)——AIDL的使用

    综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...

  2. CSS hack——不同浏览器的CSS应对法

    1.IE条件注释法: lte表示“小于等于”,“lt”表示“小于”,“gte”表示“大于等于”,“gt”表示“大于”,“!”表示“不等于”. <!--[if IE 6]> <link ...

  3. pageX,clientX,offsetX,layerX的区别

    pageX,clientX,offsetX,layerX的区别 在各个浏览器的JS中,有很多个让你十分囧的属性,由于各大厂商对标准的解释和执行不一样,导致十分混乱,也让我们这些前端攻城狮十分无语和纠结 ...

  4. 嵌入式 fork与vfork的区别

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1.  fork  ():子进程拷贝父进程的数据段,代码段     vfork ( ):子进程与父进程共享数据段 ...

  5. GitHub的代码托管和使用方法

    原文  GitHub托管 借助GitHub托管你的项目代码 PS:话说自己注册了GitHub都很久了,却没有怎么去弄,现在系统学习一下,也把自己的学习经历总结下来share给大家,希望大家都能把Git ...

  6. table应用之colspan与rowspan

    <table border=" borderColorDark="#66ff33"> <tr> <td rowspan=" ali ...

  7. Golang 做的几个跟性能有关的工具

    1.Vegeta https://github.com/tsenart/vegeta Vegeta 是一个万能的 HTTP 负载测试工具,提供命令行工具和一个开发包. 使用方法: $ vegeta a ...

  8. 说说Python 中的文件操作 和 目录操作

    我们知道,文件名.目录名和链接名都是用一个字符串作为其标识符的,但是给我们一个标识符,我们该如何确定它所指的到底是常规文件文件名.目录名还是链接名呢?这时,我们可以使用os.path模块提供的isfi ...

  9. MVC同一页面循环显示数据库记录(答题/投票系统)

    ) { //int id = 1; list newlist = db.lists.Find(id); //var q = from p in db.lists where p.id==1 selec ...

  10. 客户端无法tcp连接上本地虚拟机的问题(最后是linux防火墙问题)

    刚装好裸的centos6.5,很多东西跟以前比都是没有的,所以做起来会遇到很多问题. 今天刚把svn 无法ci的问题解决了,起完服后,发现客户端连不上. 1)端口转发,查看了一下虚拟机的端口转发,发现 ...