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>