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