成都达内首页 >> java培训 >> 三种json处理java类库
三种json处理java类库
作者:达内 更新时间:2012-06-11 09:34:01 来源:达内科技
【成都java培训】json格式非常灵活,希望用在变量传递上,json分析处理类库网上很多,以前用过json-lib.jar也没太注意,后来有需求要求分析速度非常快,而且处理的json格式兼容性非常强,于是遇到了jackson json 及smatt json的相关介绍。由于我只需要简单的json格式分析功能,不需要太复杂的格式化到Bean、POJO等功能,所以就这几个类库做了个测试:成都达内整理
jsp页面测试代码
Java代码
1.<%@page contentType="text/html;charset=GBK" import="net.xerllent.doc.convert.*,net.sf.json.* "%>
2.<%@page import="org.apache.commons.beanutils.PropertyUtils"%>
3.<%@page import="net.xerllent.doc.Config"%>
4.<%@page import="java.util.Date"%>
5.<%@page import="java.sql.Timestamp"%>
6.<%@page import="org.codehaus.jackson.JsonParser"%>
7.<%@page import="org.codehaus.jackson.JsonFactory"%>
8.<%@page import="org.codehaus.jackson.JsonNode"%>
9.<%@page import="java.util.Map"%>
10.<%@page import="org.codehaus.jackson.map.ObjectMapper"%>
11.<%@page import="net.minidev.json.JSONValue"%>
12.<%
13.try {
14.
15. long l0 =0l;int k=1000000;
16. String json = "{\"name\":\"jsjjon\",\"age\":435,\"param3\":\"赛灵通项目(Xerllent Projects)是一项基于j2ee技术的企业信息化系统研发计划,这项计划的目的是让更多企业享受简洁易用、安全可靠、功能强大的信息化软件平台\",\"param4\":435,\"param5\":435,\"param6\" : 435, \"param7\":435, \"parasdfdsfam8\":\"sdjfldsjfdsljfldsjflkdsfdslkjflsdjfju4385\"}";
17.
18. out.print("smart json test:<br>");
19. l0=System.currentTimeMillis();
20. Object obj1;net.minidev.json.JSONObject obj3=null;
21. for(int i=0;i<k;i++){
22. obj1=JSONValue.parse(json);
23. obj3=(net.minidev.json.JSONObject)obj1;
24. }
25. out.print("name:"+obj3.get("name")+"<br>");
26. out.print("age:"+obj3.get("age")+"<br>");
27. out.println("smart json cost milli seconds:"+(System.currentTimeMillis()-l0));
28. out.print("<br>");
29.
30.
31. out.print("<br>");
32. out.print("<br>");
33.
34.
35. out.print("net.sf.json lib test:<br>");
36. l0=System.currentTimeMillis();
37. JSONObject obj=null;
38. for(int i=0;i<k;i++){
39. obj=JSONObject.fromObject(json);
40. }
41. out.print("name:"+obj.get("name")+"<br>");
42. out.print("age:"+obj.get("age")+"<br>");
43. out.println("net.sf.json lib cost milli seconds:"+(System.currentTimeMillis()-l0));
44. out.print("<br>");
45.
46.
47. out.print("<br>");
48. out.print("<br>");
49.
50. out.print("jackson json lib test:<br>");
51. l0=System.currentTimeMillis();
52. ObjectMapper m=null;JsonNode rootNode=null;
53. for(int i=0;i<k;i++){
54. m = new ObjectMapper();
55. rootNode = m.readValue(json, JsonNode.class);
56. }
57. out.print("name:"+rootNode.get("name")。asText()+"<br>");
58. out.print("age:"+rootNode.get("age")。asText()+"<br>");
59. out.println("net.sf.json lib cost milli seconds:"+(System.currentTimeMillis()-l0));
60. out.print("<br>");
61.
62.
63.}
64.catch (Exception ex) {
65. System.err.println("aaaaaaaaa:"+ex);
66.}
<%@page contentType="text/html;charset=GBK" import="net.xerllent.doc.convert.*,net.sf.json.* "%>
<%@page import="org.apache.commons.beanutils.PropertyUtils"%>
<%@page import="net.xerllent.doc.Config"%>
<%@page import="java.util.Date"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="org.codehaus.jackson.JsonParser"%>
<%@page import="org.codehaus.jackson.JsonFactory"%>
<%@page import="org.codehaus.jackson.JsonNode"%>
<%@page import="java.util.Map"%>
<%@page import="org.codehaus.jackson.map.ObjectMapper"%>
<%@page import="net.minidev.json.JSONValue"%>
<%
try {
long l0 =0l;int k=1000000;
String json = "{\"name\":\"jsjjon\",\"age\":435,\"param3\":\"赛灵通项目(Xerllent Projects)是一项基于j2ee技术的企业信息化系统研发计划,这项计划的目的是让更多企业享受简洁易用、安全可靠、功能强大的信息化软件平台\",\"param4\":435,\"param5\":435,\"param6\" : 435, \"param7\":435, \"parasdfdsfam8\":\"sdjfldsjfdsljfldsjflkdsfdslkjflsdjfju4385\"}";成都java培训
out.print("smart json test:<br>");
l0=System.currentTimeMillis();
Object obj1;net.minidev.json.JSONObject obj3=null;
for(int i=0;i<k;i++){
obj1=JSONValue.parse(json);
obj3=(net.minidev.json.JSONObject)obj1;
}
out.print("name:"+obj3.get("name")+"<br>");
out.print("age:"+obj3.get("age")+"<br>");
out.println("smart json cost milli seconds:"+(System.currentTimeMillis()-l0));
out.print("<br>");
out.print("<br>");
out.print("<br>");
out.print("net.sf.json lib test:<br>");
l0=System.currentTimeMillis();
JSONObject obj=null;
for(int i=0;i<k;i++){
obj=JSONObject.fromObject(json);
}
out.print("name:"+obj.get("name")+"<br>");
out.print("age:"+obj.get("age")+"<br>");
out.println("net.sf.json lib cost milli seconds:"+(System.currentTimeMillis()-l0));
out.print("<br>");
out.print("<br>");
out.print("<br>");
out.print("jackson json lib test:<br>");
l0=System.currentTimeMillis();
ObjectMapper m=null;JsonNode rootNode=null;
for(int i=0;i<k;i++){
m = new ObjectMapper();
rootNode = m.readValue(json, JsonNode.class);
}
out.print("name:"+rootNode.get("name")。asText()+"<br>");
out.print("age:"+rootNode.get("age")。asText()+"<br>");
out.println("net.sf.json lib cost milli seconds:"+(System.currentTimeMillis()-l0));
out.print("<br>");
}
catch (Exception ex) {
System.err.println("aaaaaaaaa:"+ex);
}
测试结果:100万次测试结果:
Html代码
1.smart json test:
2.name:jsjjon
3.age:435
4.smart json cost milli seconds:3515
5.
6.
7.net.sf.json lib test:
8.name:jsjjon
9.age:435
10.net.sf.json lib cost milli seconds:21250
11.
12.
13.jackson json lib test:
14.name:jsjjon
15.age:435
16.net.sf.json lib cost milli seconds:19391
smart json test:
name:jsjjon
age:435
smart json cost milli seconds:3515
net.sf.json lib test:
name:jsjjon
age:435
net.sf.json lib cost milli seconds:21250
jackson json lib test:
name:jsjjon
age:435
net.sf.json lib cost milli seconds:19391
测试结论:
看来smart json 果然快,而且json格式兼容性特强,甚至有些Bt,但其他附加功能还不叫弱,不过对我来说已经够了。先用着吧。
成都java培训官网:
上一篇:浅析Hibernate实现对象持久化
下一篇:java中乱码或者识别中文方法
相关信息