JSON parsing tool Jackson (simple Application)

Source: Internet
Author: User

Overview
Jackson Library (http://jackson.codehaus.org), a java-based open source JSON format parsing tool, contains 3 jar packages for the entire library (using the latest version 2.2):
the Jackson-core.jar--core Package (required) provides an API based on "stream mode" parsing.
the Jackson-databind--data Binding Package (optional) provides APIs based on the object binding and tree Model.
The Jackson-annotations--annotation package (optional) provides annotation Functionality.
Jackson's Advantages
compared to other libraries parsed by Java json, such as the json-lib, Gson package, Jackson has the following advantages:
1. Comprehensive function, provide a variety of modes of JSON parsing, "object binding" easy to use, the use of annotations package can provide a lot of convenience for our Development.
2. High performance, "flow mode" parsing efficiency more than most similar JSON package.
Important API
Core package: Jsonpaser (json stream read), jsongenerator (json stream output).
Data binding package: Objectmapper (build tree Mode and object binding mode), Jsonnode (tree node).
Simple Example
in practice, most of the data object binding patterns are used, an object is serialized as a JSON string, and a string of JSON strings are deserialized into a Java object or map.

Person Class:

 public classperson {PrivateString name; PrivateInteger age; //necessary for     publicperson () {} publicperson (String name, Integer age) { this. Name =name;  this. Age =age ; }     publicString getName () {returnname; }     public voidsetName (String Name) { this. Name =name; }     publicInteger getage () {returnage ; }     public voidsetage (Integer Age) { this. Age =age ; }}

Test class

/*** The example itself is simple, but the writing in the main method is well worth learning from the idea of refactoring*/ public classDemo { public Static voidmain (string[] Args) {//Writejsonobject (); //Readjsonobject ();Readjsonmap (); }    //write directly to an object (serialized)     public Static voidwritejsonobject () {objectmapper mapper=NewObjectmapper (); person Person=NewPerson ("nomouse", 25); Try{mapper.writevalue (NewFile ("c:/person.json"), person); String String=mapper.writevalueasstring (person);        System.out.println (string); } Catch(jsongenerationexception E) {e.printstacktrace (); } Catch(jsonmappingexception E) {e.printstacktrace (); } Catch(ioexception E) {e.printstacktrace (); }    }    //converts a JSON directly into an object (deserialization)     public Static voidreadjsonobject () {objectmapper mapper=NewObjectmapper (); Try{ person person= Mapper.readvalue (NewFile ("c:/person.json"), Person.class); System.out.println (person.getname ()+ ":" +Person.getage ()); String String= "{\" name\ ": \" nomouse\ ", \" age\ ": 25}"; //person must have an Argument-free construction methodPerson Person1 = Mapper.readvalue (string, Person.class); System.out.println (person1.getname ()+ ":" +Person1.getage ()); } Catch(jsonparseexception E) {e.printstacktrace (); } Catch(jsonmappingexception E) {e.printstacktrace (); } Catch(ioexception E) {e.printstacktrace (); }    }    //Direct conversion to map     public Static voidreadjsonmap () {objectmapper mapper=NewObjectmapper (); Try {            //it is important to note that the map here is actually a likedhashmap, a chain hash table, which can be traversed in the read-in order .Map map = Mapper.readvalue (NewFile ("c:/person.json"), Map.class); System.out.println (map.get ("name") + ":" + map.get ("age")); String String= "{\" name\ ": \" nomouse\ ", \" age\ ": 25}"; //person must have an Argument-free construction methodMap Map1 = Mapper.readvalue (string, Map.class);        System.out.println (map1); } Catch(jsonparseexception E) {e.printstacktrace (); } Catch(jsonmappingexception E) {e.printstacktrace (); } Catch(ioexception E) {e.printstacktrace (); }    }}

JSON parsing tool Jackson (simple Application)

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.