1,user Object
PackageCom.st.json;Importjava.util.Date;/*** @Description: JSON serialization and deserialization using the user class *@authormr.li * @date April 21, 2018 PM 10:55:34*/ Public classUser {PrivateInteger ID; PrivateString name; PrivateInteger age; PrivateDate birthday; PrivateString Email; PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicDate Getbirthday () {returnbirthday; } Public voidsetbirthday (Date birthday) { This. Birthday =birthday; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + ", age=" + Age + ", birthday=" + Birthday + ", email=" +Email+ "]"; } PublicUser (integer ID, string name, Integer age, Date Birthday, string email) {Super(); This. ID =ID; This. Name =name; This. Age =Age ; This. Birthday =birthday; This. email =email; } PublicUser () {Super(); //TODO auto-generated Constructor stub }}
2,jacksonutil Tool Class
PackageCom.st.json;Importjava.io.IOException;ImportJava.io.StringWriter;Importcom.fasterxml.jackson.core.JsonFactory;ImportCom.fasterxml.jackson.core.JsonGenerator;Importcom.fasterxml.jackson.core.JsonParseException;Importcom.fasterxml.jackson.databind.JsonMappingException;ImportCom.fasterxml.jackson.databind.ObjectMapper;/*** @Description: Jackson object and JSON Transfer tool class *@authorMr.li * @date April 22, 2018 morning 12:30:15*/ Public classJacksonutil {Private StaticObjectmapper mapper =NewObjectmapper (); Public StaticString Beantojson (Object obj)throwsIOException {stringwriter SW=NewStringWriter (); Jsongenerator Gen=Newjsonfactory (). CreateGenerator (SW); Mapper.writevalue (gen, obj); Gen.close (); returnsw.tostring (); } Public Static<T> T Jsontobean (String jsonstr, class<t>objclass)throwsjsonparseexception, Jsonmappingexception, IOException {returnMapper.readvalue (Jsonstr, objclass); }}
3,jackson object and JSON mutual transfer test
PackageCom.st.json;Importjava.io.IOException;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.List;Importcom.fasterxml.jackson.core.JsonParseException;Importcom.fasterxml.jackson.databind.JsonMappingException;/*** @Description: Jackson object and JSON transfer test *@authorMr.li * @date April 22, 2018 morning 12:39:39*/ Public classJacksonutiltest { Public Static voidMain (string[] args)throwsParseException, IOException {beantojack ();//Jsontobean (); } Public Static voidBeantojack ()throwsParseException, IOException {User u=NewUser (); U.setid (1); U.setname ("Curry"); U.setage (30); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); U.setbirthday (Dateformat.parse ("1988-9-21")); U.setemail ("[Email protected]"); User U2=NewUser (); U2.setid (2); U2.setname ("KD"); U2.setage (29); U2.setbirthday (Dateformat.parse ("1989-9-21")); U2.setemail ("[Email protected]"); List<User> users =NewArraylist<>(); Users.add (U); Users.add (U2); //Object goto JSON formatString Beantojson =Jacksonutil.beantojson (users); System.out.println (Beantojson); } Public Static voidJsontobean ()throwsjsonparseexception, Jsonmappingexception, IOException {String json= "{\" id\ ": 3, \" name\ ": \" Xiao Ming \ ", \" age\ ":, \" birthday\ ": 590774400000, \" email\ ": \" [Email protected]\ "}"; User Jsontobean= Jacksonutil.jsontobean (JSON, User.class); System.out.println (Jsontobean); }}
Jackson object and JSON data transfer tool class Jacksonutil