读书人

Jackson 高性能的JSON处置 android/移

发布时间: 2012-12-31 11:57:52 作者: rapoo

Jackson 高性能的JSON处理 android/移动开发必备jackson .

今天自行研究了下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、 测试类基本代码如下

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

打印结果 :

串转换成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}}

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

[java] view plaincopy
  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. public class Student {
  11. /*
  12. *
  13. * Class Descripton goes here.
  14. *
  15. * @class Student
  16. * @version 1.0
  17. * @author 廖益平
  18. * @time 2011-11-8 上午03:01:08
  19. */
  20. private int uid;
  21. private String uname;
  22. private String upwd;
  23. private double number;
  24. private boolean isstudent;
  25. public int getUid() {
  26. return uid;
  27. }
  28. public void setUid(int uid) {
  29. this.uid = uid;
  30. }
  31. public String getUname() {
  32. return uname;
  33. }
  34. public void setUname(String uname) {
  35. this.uname = uname;
  36. }
  37. public String getUpwd() {
  38. return upwd;
  39. }
  40. public void setUpwd(String upwd) {
  41. this.upwd = upwd;
  42. }
  43. public double getNumber() {
  44. return number;
  45. }
  46. public void setNumber(double number) {
  47. this.number = number;
  48. }
  49. public boolean isIsstudent() {
  50. return isstudent;
  51. }
  52. public void setIsstudent(boolean isstudent) {
  53. this.isstudent = isstudent;
  54. }
  55. @Override
  56. public String toString() {
  57. return "uid="+uid+",name="+uname+",upwd="+upwd+",number="+number+",isStudent="+isstudent;
  58. }
  59. }

读书人网 >移动开发

热点推荐