Packagecom.zving;ImportOrg.codehaus.jackson.map.ObjectMapper;/*** Learn Objectmapper class * *@authorClover*/ Public classJacksontester { Public Static voidMain (string[] args) {objectmapper mapper=NewObjectmapper (); String jsonstring= ""; Stringtostudent (mapper, jsonstring); Studenttostring (mapper, jsonstring); Stringtostudentlist (mapper, jsonstring); } /*** String to entity class * *@paramMapper *@paramjsonstring*/ Public Static voidstringtostudent (objectmapper mapper, String jsonstring) {jsonstring= "{\" id\ ": 1001,\" name\ ": \" tom\ ", \" info\ ": \" a little Boy \ "}"; Try{Student Student= Mapper.readvalue (jsonstring, Student.class); SYSTEM.OUT.PRINTLN (student); } Catch(Exception e) {e.printstacktrace (); } } /*** Entity class go to String * *@paramMapper *@paramjsonstring*/ Public Static voidstudenttostring (objectmapper mapper, String jsonstring) {Student Student=NewStudent (); Student.setid (1002); Student.setname ("John Doe"); Student.setinfo ("A little girl."); Try{jsonstring=mapper.writevalueasstring (student); System.out.println (jsonstring); } Catch(Exception e) {e.printstacktrace (); } } /*** String to list< entity class > * *@paramMapper *@paramjsonstring*/ Public Static voidstringtostudentlist (objectmapper mapper, String jsonstring) {jsonstring= "[{\" id\ ": 1001,\" name\ ": \" tom\ ", \" info\ ": \" a little boy \ "},{\" id\ ": 1002,\" name\ ": \" jane\ ", \" info\ ": \" a little girl \ "}]"; Try{student[] stu= Mapper.readvalue (jsonstring, student[].class); for(Student student:stu) {System.out.println (Student); } String SS=mapper.writevalueasstring (STU); SYSTEM.OUT.PRINTLN (ss); } Catch(Exception e) {e.printstacktrace (); } }}
Java Operation Objectmapper Class