Jackson Objectmapper Class Usage parsing

Source: Internet
Author: User

/*** Jackson Objectmapper class*///the Objectmapper class is the main class for the Jackson Library. It provides some functionality that will translate into Java objects to match the JSON structure, and vice versa. It uses instances of Jsonparser and jsongenerator to implement JSON actual read/write. //The following is a declaration of the Org.codehaus.jackson.map.ObjectMapper class: Public classObjectmapperextendsObjectcodecImplementsversioned//Demo Example: Importjava.io.IOException;Importorg.codehaus.jackson.JsonParseException;Importorg.codehaus.jackson.map.JsonMappingException;ImportOrg.codehaus.jackson.map.ObjectMapper;ImportOrg.codehaus.jackson.map.SerializationConfig; Public classJacksontester { Public Static voidMain (String args[]) {objectmapper mapper=NewObjectmapper (); String jsonstring= "{\" name\ ": \" mahesh\ ", \" age\ ": 21}"; //map JSON to student      Try{Student Student= Mapper.readvalue (jsonstring, Student.class);         SYSTEM.OUT.PRINTLN (student);         Mapper.enable (SerializationConfig.Feature.INDENT_OUTPUT); //turns the object into a JSON string typeJsonstring =mapper.writevalueasstring (student);      System.out.println (jsonstring); } Catch(jsonparseexception e) {e.printstacktrace (); } Catch(jsonmappingexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }   }}classStudent {PrivateString name; Private intAge ;  PublicStudent () {} PublicString GetName () {returnname; }    Public voidsetName (String name) { This. Name =name; }    Public intGetage () {returnAge ; }    Public voidSetage (intAge ) {       This. Age =Age ; }    PublicString toString () {return"Student [Name:" +name+ ", Age:" + age+ "]"; }    }//Output ResultsStudent [Name:mahesh, age:21 ]{  "Name": "Mahesh",  "Age": 21}

Jackson Objectmapper Class Usage parsing

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.