Jackson transforms the JSON string into list<javabean>

Source: Internet
Author: User

 Public FinalObjectmapper mapper =NewObjectmapper ();  Public Static voidMain (string[] args)throwsexception{String jsonstring= Getjsonstring();//GetConfig omitted//list<config> configlist = (list<config>) jsonstring//The above conversion is wrong, but the compilation is not an error, the runtime only errorJavatype Javatype= Getcollectiontype (ArrayList.class, Config.class); List<Config> configlist = Mapper.readvalue (jsonstring, Javatype);//There is no need to cast}   /*** Gets the collection type of the generic type *@paramcollectionclass Generic Collection *@paramelementclasses Element class *@returnjavatype Java Type *@since1.0*/    Public StaticJavatype Getcollectiontype (class<?> collectionclass, class<?>... elementclasses) {       returnmapper.gettypefactory (). Constructparametrictype (Collectionclass, elementclasses); }

Pay attention to the last method.

Jackson transforms the JSON string into list<javabean>

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.