Analysis of several JSON tools in Java
1, Environment
Jdk1.6+ide (IntelliJ idea) +WINDOWSXP+GBK encoding
2 , analyze Objects
jackson1.8.2 http://jackson.codehaus.org/
gson1.7.1 http://code.google.com/p/google-gson/
jsob_lib2.4 http://json-lib.sourceforge.NET/
3 , using an instance
tested with two beans, all two beans are nested with arrays and objects, the only difference being that one property starts with all lowercase, the other property starts all uppercase, and the bean has a setter and getter.
Jbean The code is as follows:
[Java]View Plain Copy
- public class Jbean {
- private int b_int;
- Private String b_string;
- Private List b_list;
- Private JBEANSUB1 b_object;//Child object
- private static final String JDate = "{/" b_int/": 1988,/" b_string/":/" sheep/",/" b_list/": [/" list1/",/" list2/",/" list3/ "],/" b_object/": {/" sub_int/": 2012}";
- public void Initbean () {
- This.setb_int (1988);
- This.setb_string ("sheep");
- This. B_list = new ArrayList ();
- This. B_object = new JBeanSub1 ();
- This. B_list.add ("List1");
- This. B_list.add ("List2");
- This. B_list.add ("List3");
- This. B_object.setsub_int (2012);
- }
- Getter and setter ...
- }
- Class jbeansub1{
- private int sub_int;
- Getter and setter ...
- }
JBean2 The code is as follows:
Change the Jbean property header letters to lowercase, and then use the IDE to automatically generate setter and getter.
Jackson working with Instances
(Json to Bean)
[Java]View Plain Copy
- Objectmapper mapper = new Objectmapper ();
- Jbean bean = Mapper.readvalue (Jbean.getjdate (), jbean.class);
(Bean to JSON)
[Java]View Plain Copy
- Jbean bean = new Jbean ();
- Bean.initbean ();
- StringWriter SW = new StringWriter ();
- Jsongenerator Gen = new Jsonfactory (). Createjsongenerator (SW);
- Mapper.writevalue (gen, Bean);
- Gen.close ();
- String json = sw.tostring ();
Nested with list and object, Jackson can still convert exactly, the only disadvantage is that regardless of whether the bean attribute begins with uppercase or lowercase, the first letter is lowercase when Jackson is converted to a string.
Gson working with Instances
(JSON to Bean)
[Java]View Plain Copy
- Gson Gson = new Gson ();
- JBean2 bean = Gson.fromjson (Jbean.getjdate (), jbean2.class);
(Bean to JSON)
[Java]View Plain Copy
- JBean2 bean = new JBean2 ();
- Bean.initbean ();
- System.out.println (Gson.tojson (bean));
It is very simple to use, when the bean lowercase letter begins, the JSON goes to the bean to succeed.
Json_lib working with Instances
(JSON to Bean)
[Java]View Plain Copy
- JBean2 bean = new JBean2 ();
- Bean.initbean ();
- Jsonobject obj = Jsonobject.fromobject (bean);
- System.out.println (Jsonarray.fromobject (Bean). toString ());
(Bean to JSON)
[Java]View Plain Copy
- JBean2 bean = new JBean2 ();
- Bean.initbean ();
- System.out.println (Jsonobject.fromobject (bean));
- System.out.println (Jsonarray.fromobject (Bean). toString ());
Support for nested list and object is not good enough, I did not succeed in debugging nested, time relationship did not have a chance to find out why, but before the use of this tool, the greatest benefit is that whether the JSON string nested arrays or objects, Using Jsonobject.fromobject or Jsonarray.fromobject conversion is converted into objects and arrays, which is convenient for operation, but changes the original data structure of JSON.
4 , performance comparison
Previously did not do performance testing such work, so online search for the results of the research, address http://wangym.iteye.com/blog/738933
In short, the performance side is Jackson > Gson > Json-lib. The performance of Gson is slightly higher than that of Json-lib, and the jacks performance is 10 times times higher than Json-lib.
5 , summarize
One more thing to forget, Jackson and Gson can be used independently, while Json-lib is dependent on the other five packages, Json-lib official website
Json-lib requires (at least) the following dependencies in your classpath:
Jakarta Commons-lang 2.5
Jakarta commons-beanutils 1.8.0
Jakarta commons-collections 3.2.1
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6
These class libraries are said to be in struts2. No language json-lib, low performance, but also rely on so many kinds of libraries, conversion is not very convenient.
Strongly recommend the use of Jackson and Gson, I test the source code http://download.csdn.net/source/3386315, the time is too hasty simple summary, I hope the next time to meet the JSON can reflect the rapid point, what is wrong to hope to correct, Thank you!
Comparison of JSON tools in Java