Objective:
Jackson Common methods:
Jackson is the usual two ways to handle the conversion between generic Java objects and JSON are the writevalueasstring () and ReadValue () methods of the Objectmapper class that can be implemented.
However, if it is to be turned into a list collection of the specified generics, such as list<user>. You need to deserialize the complex type to generics first
Collection Type;
To illustrate:
If it is a list<user> collection, then use Objectmapper's gettypefactory (). Constructparametrictype (List.class, User.class);
If it is a hashmap<string,user> collection, then use Objectmapper's gettypefactory (). Constructparametrictype (HashMap.class, String.class,user. Class);
code implementation:
Public final objectmapper mapper = new objectmapper (); public static void main (String[] args) throws exception{ javatype javatype = getcollectiontype (Arraylist.class, yourbean.class); List<YourBean> lst = (list<yourbean>) Mapper.readvalue (jsonstring, Javatype); } /** * get collection type of generics * @param collectionClass Generics collection * @param elementClasses Element Classes * @return javatype java Type * @since 1.0 */ public static javatype getcollectiontype ( class<?> collectionclass, class<?>... elementclasses) { return mapper.gettypefactory (). ConstructParametricType ( collectionclass, elementclasses); }
This article is from the "Simple Life" blog, so be sure to keep this source http://simplelife.blog.51cto.com/9954761/1704106
How Jackson implements a conversion between a JSON string and a generic list collection