Several simple transformations of the Jackson complex object collection

Source: Internet
Author: User

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

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.