/*** 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