Guidelines for selecting Java JSON libraries

Source: Internet
Author: User

For the company to do a small task, need to use the Java JSON Library, JSON library I used a few months ago, but then was followed by the project, the continuation of the project's usage habits directly with the Jackson JSON, and this time I think a good comparison of several common JSON library, and then choose the fastest.

Read a few blog, think in fact can choose three kinds, Jackson, Gson, Json.simple. I used the json.simple first, and then I wrote out a function like this.

Reads JSON data from the URL and adds identity information to the request

     Public Staticjsonobject readjsonfromurl (string URL, string token) {Try{URLConnection conn=Newurl (url). OpenConnection (); Conn.setrequestproperty ("Authorization", "Bearer" +token); InputStream Response=Conn.getinputstream (); String Jsonstr=ioutils.tostring (response); Jsonobject Jsonobj=(Jsonobject) jsonvalue.parsewithexception (JSONSTR);//System.out.println (jsonobj);            returnJsonobj; }Catch(Exception e) {e.printstacktrace (); }        return NULL; }

Then, extract the data from the JSON

     Public Static Reply Parsejson (jsonobject jsonobj) {        new Reply (Jsonobj.get ("Next_stream_position"). ToString ());         = (Jsonarray) (Jsonobj.get ("entries"));          for (int i = 0; i < jsonary.size (); i + +)            {= (jsonobject) jsonary.get (i);            Reply.addipaddr (Iobj.get ("IP_Address"). toString ());        }         return reply;    }

After writing, I felt the need to replace JSON with a more efficient implementation, so I'm going to pick from Jackson, Gson

Jackson is suitable for handling large amounts of data, Gson is suitable for smaller data, and the Gson jar package is a lot smaller than other options, giving me a very light feeling.

In addition, I am reluctant to write a pojo for JSON data, as I only have one or two member variables in this pojo. Not only did Jackson want this pojo, but he also needed to add annotation to the Pojo, so I just ruled it out.

Gson,google's products give me a sense of trust. So I wrote this code.

     Public StaticReply readjsonfromurl (string URL, string token) {Reply Reply=NewReply (); Try{URLConnection conn=Newurl (url). OpenConnection (); Conn.setrequestproperty ("Authorization", "Bearer" +token); InputStream Response=Conn.getinputstream (); Jsonreader Reader=NewJsonreader (NewInputStreamReader (Response, "UTF-8")); //Only one element would be returned//Parse Data recursively//Google's handle streaming data, ugly thoughReader.beginobject ();            Reader.beginobject ();  while(Reader.hasnext ()) {String name=Reader.nextname (); if(Name.equals ("Next_stream_position")) Reply.stream_position=reader.nextstring (); Else if(Name.equals ("entries") ) {Reader.beginarray ();  while(Reader.hasnext ()) {reader.beginobject ();  while(Reader.hasnext ()) {name=Reader.nextname (); if(Name.equals ("IP_Address") ) reply.addipaddr (reader.nextstring ()); Else if(Name.equals ("Created_at")) Reply.lastdate=reader.nextstring (); ElseReader.skipvalue ();                    } reader.endobject ();                } reader.endarray (); } ElseReader.skipvalue ();            } reader.endobject ();            Reader.close (); returnreply; }Catch(Exception e) {e.printstacktrace (); }        return NULL; }

Because of the need to deal with streaming data and do not want to write Pojo, eventually written in this ugly piece of code, I think the efficiency can be guaranteed.

After writing the code, I think the most appropriate JSON library should still be json.simple, its conversion is simple, can be directly based on the string get to the desired information, and the most important is the short code write, the implementation of high efficiency. In the enterprise, in the school, in the face of the intersection anywhere, with the shortest time to complete the task is the most important, which I have deep experience.

Well, the default with Json.simple

Guidelines for selecting Java JSON libraries

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.