Comparison of JSON tools in Java

Source: Internet
Author: User

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
  1. public class Jbean {
  2. private int b_int;
  3. Private String b_string;
  4. Private List b_list;
  5. Private JBEANSUB1 b_object;//Child object
  6. private static final String JDate = "{/" b_int/": 1988,/" b_string/":/" sheep/",/" b_list/": [/" list1/",/" list2/",/" list3/ "],/" b_object/": {/" sub_int/": 2012}";
  7. public void Initbean () {
  8. This.setb_int (1988);
  9. This.setb_string ("sheep");
  10. This. B_list = new ArrayList ();
  11. This. B_object = new JBeanSub1 ();
  12. This. B_list.add ("List1");
  13. This. B_list.add ("List2");
  14. This. B_list.add ("List3");
  15. This. B_object.setsub_int (2012);
  16. }
  17. Getter and setter ...
  18. }
  19. Class jbeansub1{
  20. private int sub_int;
  21. Getter and setter ...
  22. }

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
    1. Objectmapper mapper = new Objectmapper ();
    2. Jbean bean = Mapper.readvalue (Jbean.getjdate (), jbean.class);

(Bean to JSON)

[Java]View Plain Copy
    1. Jbean bean = new Jbean ();
    2. Bean.initbean ();
    3. StringWriter SW = new StringWriter ();
    4. Jsongenerator Gen = new Jsonfactory (). Createjsongenerator (SW);
    5. Mapper.writevalue (gen, Bean);
    6. Gen.close ();
    7. 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
    1. Gson Gson = new Gson ();
    2. JBean2 bean = Gson.fromjson (Jbean.getjdate (), jbean2.class);

(Bean to JSON)

[Java]View Plain Copy
    1. JBean2 bean = new JBean2 ();
    2. Bean.initbean ();
    3. 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
    1. JBean2 bean = new JBean2 ();
    2. Bean.initbean ();
    3. Jsonobject obj = Jsonobject.fromobject (bean);
    4. System.out.println (Jsonarray.fromobject (Bean). toString ());

(Bean to JSON)

[Java]View Plain Copy
    1. JBean2 bean = new JBean2 ();
    2. Bean.initbean ();
    3. System.out.println (Jsonobject.fromobject (bean));
    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.