Jackson JSON and objects, several transformations of the collection

Source: Internet
Author: User

Jackson JSON and objects, several transformations of the collection

Package Com.crm.net.util;import Java.io.bufferedreader;import Java.io.bytearrayinputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.util.list;import Org.codehaus.jackson.jsonparseexception;import Org.codehaus.jackson.map.deserializationconfig;import Org.codehaus.jackson.map.jsonmappingexception;import Org.codehaus.jackson.map.objectmapper;import Org.codehaus.jackson.type.javatype;public class Jsonutils<t> {//objectmapper mapper2 = new Objectmapper ();// Logininfo user = Mapper2.readvalue (result.tostring (), Logininfo.class),//system.out.println ("User:" + user);/** * converted To List * * @param josn * @param clz * @return */public list<t> jsonconverlist (String josn, Class clz) {Objectmapper J Acksonmapper = new Objectmapper (); 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 = Jacksonmapper.gettypefactory (). Constructparametrictype (List.class, CLZ);//Clz.selGenType (). GetClass () Me = Jacksonmapper.readvalue (Josn, javatype);} catch (Jsonparseexception e) {e.printstacktrace ();} catch (Jsonmappingexception e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} return me;} /** * Conversion object contains JSON * * @param josn * @param clz * @return */public T jsonconverobject (String josn, class<t> clz) {T t = null;//objectmapper jacksonmapper = new Objectmapper (); InputStreamReader in = new InputStreamReader (New ByteArrayInpu TStream (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;}}


Jackson JSON and objects, several transformations of the 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.