Several simple transformations of the Jackson complex object collection
Package Com;import Java.io.bufferedreader;import Java.io.bytearrayinputstream;import java.io.ioexception;import Java.io.inputstreamreader;import Java.util.list;import Com.fasterxml.jackson.core.jsonparseexception;import Com.fasterxml.jackson.databind.javatype;import Com.fasterxml.jackson.databind.jsonmappingexception;import com.fasterxml.jackson.databind.objectmapper;/** * Several simple transformations of the Jackson complex object collection * @author Lenovo * * @param <T> */public C Lass main<t>{static objectmapper mapper = new Objectmapper ();p ublic static void Main (string[] args) throws Jsonparse Exception,jsonmappingexception, ioexception{string josn = "{\" userid\ ": 1,\" loginname\ ": \" storegift \ ", \" truename\ ": \" Super \ ", \ "nickname\": Null,\ "loginpwd\": \ "e10adc3949ba59abbe56e057f20f883e\", \ "qq\": \ "\", \ "phone\": \ "\", \ "email\": null, \ "Remark\": \ "\", \ "account_non_locked\": 0,\ "telelephone\": Null,\ "isdelete\": 0} "; User U = Mapper.readvalue (Josn, user.class);//user U=new main<user> (). Jsonstreamconverobject (Josn, User.class); System.ouT.println ("Go to object:" + u);//Turn collection string JOSN2 = "[{\" userid\ ": 1,\" loginname\ ": \" storegift \ ", \" truename\ ": \" super \ ", \" nickname\ ": Null,\ "loginpwd\": \ "e10adc3949ba59abbe56e057f20f883e\", \ "qq\": \ "\", \ "phone\": \ "\", \ "email\": Null,\ "Remark\": \ " \ ", \" account_non_locked\ ": 0,\" telelephone\ ": Null,\" isdelete\ ": 0}]"; Javatype Javatype = Mapper.gettypefactory (). Constructparametrictype (List.class, User.class); list<user> me = Mapper.readvalue (JOSN2, Javatype); System.out.println ("Go to Collection Me:" + me);//object with set conversion string josn3 = "{\" userid\ ": 1,\" loginname\ ": \" storegift \ ", \" truename\ ": \" Super \ ", \ "nickname\": Null,\ "loginpwd\": \ "e10adc3949ba59abbe56e057f20f883e\", \ "qq\": \ "\", \ "phone\": \ "\", \ "email\": null, \ "Remark\": \ "\", \ "account_non_locked\": 0,\ "telelephone\": Null,\ "isdelete\": 0,\ "rolelist\": [{\ "roleid\": 0,\ " Name\ ": \" Super admin \ "\" show_name\ ": \" Super admin \ "\" Remark\ ": Null,\" type\ ": 1}]}"; User U3 = Mapper.readvalue (Josn3, User.class); Simple way//User u3=new main<user> (). Jsonconverobject (Josn3, User.class); Flow mode System.out.println ("Go to the object with a setU3: "+ U3);//Collection Object collection Convert String josn4 =" [{\ "userid\": 1,\ "loginname\": \ "storegift \", \ "truename\": \ "super \", \ "nickname\": null,\ " Loginpwd\ ": \" e10adc3949ba59abbe56e057f20f883e\ ", \" qq\ ": \" \ ", \" phone\ ": \" \ ", \" email\ ": Null,\" Remark\ ": \" \ ", \" account_non_locked\ ": 0,\" telelephone\ ": Null,\" isdelete\ ": 0,\" rolelist\ ": [{\" roleid\ ": 0,\" name\ ": \" Super admin \ ", \" Show_name\ ": \" Super admin \ ", \" Remark\ ": Null,\" type\ ": 1}]},{\" userid\ ": 2,\" loginname\ ": \" storegift \ ", \" truename\ ": \" super \ ", \" Nickname\ ": null,\" loginpwd\ ": \" e10adc3949ba59abbe56e057f20f883e\ ", \" qq\ ": \" \ ", \" phone\ ": \" \ ", \" email\ ": null,\" Remark\ ": \" \ ", \" account_non_locked\ ": 0,\" telelephone\ ": Null,\" isdelete\ ": 0,\" rolelist\ ": [{\" roleid\ ": 0,\" Name\ ": \" Super admin \ ", \" show_name\ ": \" Super admin \ ", \" Remark\ ": Null,\" type\ ": 1}]}]"; Javatype javaType4 = Mapper.gettypefactory (). Constructparametrictype (List.class, User.class); list<user> list = Mapper.readvalue (Josn4, javaType4); System.out.println ("There is a set transform in the Object object in the collection:" + list);} /*** * Go to Object * @param josn * @param clz * @return */public T JsonstreamcoNverobject (String josn, class<t> clz) {T t = null;//objectmapper jacksonmapper = new Objectmapper (); inputstreamread Er in = new InputStreamReader (New Bytearrayinputstream (Josn.getbytes ())); BufferedReader streamReader = new BufferedReader (in); StringBuilder buff = new StringBuilder (); String inputstr;try{while ((inputstr = StreamReader.ReadLine ()) = null) buff.append (INPUTSTR);//Objectmapper mapper = New Objectmapper (); t = Mapper.readvalue (buff.tostring (), CLZ);} catch (IOException e) {e.printstacktrace ();} return t;} /*** * Turn Object * @param josn * @param clz * @return */public T jsonconverobject (String josn, class<t> clz) {T t = Null;tr Y{t = Mapper.readvalue (Josn, CLZ),} catch (Jsonparseexception e) {e.printstacktrace ();} catch (Jsonmappingexception e) { E.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return t;} /** * Go to Collection * @param josn * @param clz * @return */public list<t> jsonconverlist (String josn, class<t> clz) {List <T> me = null;try{//Jacksonmapper//. Disable (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);//jacksonmapper.enabledefaulttyping ();// Jacksonmapper.setvisibility (Jsonmethod.field,jsonautodetect.visibility.any);//Jacksonmapper.configure ( serializationconfig.feature.indent_output,//false);//Format//Jacksonmapper.setserializationinclusion ( JsonSerialize.Inclusion.NON_NULL);//Jacksonmapper.configure (serializationconfig.feature.fail_on_empty_beans,// FALSE); Javatype Javatype = Mapper.gettypefactory (). Constructparametrictype (List.class, CLZ);//Clz.selgentype (). GetClass () me = Mapper.readvalue (Josn, javatype);} catch (Jsonparseexception e) {e.printstacktrace ();} catch (Jsonmappingexception e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} return me;}} /** * Output: * Turn to object: User [Userid=1, Loginname= Storegift, Truename= Super, Nickname=null, loginpwd=e10adc3949ba59abbe56e057f20f883e , qq=, phone=, Email=null, remark=, Account_non_locked=0, Telelephone=null, Indate=null, isdelete=0, RoleList=null] * Turn collection Me:[user [USerid=1, Loginname= Storegift, Truename= Super, Nickname=null, loginpwd=e10adc3949ba59abbe56e057f20f883e, QQ=, Phone=, Email=null , remark=, Account_non_locked=0, Telelephone=null, Indate=null, Isdelete=0, Rolelist=null]] * Turn to the object there is a set of U3:user [UserID=1, Loginname= Storegift, Truename= Super, Nickname=null, loginpwd=e10adc3949ba59abbe56e057f20f883e, qq=, Phone=, Email=null, Remark= , Account_non_locked=0, Telelephone=null, Indate=null, isdelete=0, Rolelist=[role [Roleid=0, Name= Super admin, Show_Name= Super Administrator, Remark=null, type=1]] [The collection is the object object in which there is a set conversion: [User [Userid=1, Loginname= Storegift, Truename= Super, Nickname=null, loginpwd= e10adc3949ba59abbe56e057f20f883e, qq=, phone=, Email=null, remark=, Account_non_locked=0, Telelephone=null, Indate= NULL, isdelete=0, Rolelist=[role [Roleid=0, name= Super admin, show_name= Super admin, Remark=null, type=1]], User [userid=2, Loginname= Storegift, Truename= Super, Nickname=null, loginpwd=e10adc3949ba59abbe56e057f20f883e, qq=, Phone=, Email=null, Remark= , Account_non_locked=0, Telelephone=null, Indate=null, isdelete=0, RolElist=[role [Roleid=0, name= Super admin, show_name= Super admin, Remark=null, type=1]] [* */
Several simple transformations of the Jackson complex object collection