Jackson, I feel, is the fastest framework for converting between Java and Json. Of course, Google's Gson is also very good, but based on the performance tests of people on the Internet, it seems that Jackson is faster.
Jackson can handle the conversion between general JavaBean and Json by simply using the readValue and writeValueAsString methods of the ObjectMapper object. But if you want to convert a complex type Collection such as List
If it is ArrayList
If it is HashMap
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); } /** * 获取泛型的Collection Type * @param collectionClass 泛型的Collection * @param elementClasses 元素类 * @return JavaType Java类型 * @since 1.0 */ public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); }
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to Java's Jackson converting json strings into generic Lists, please pay attention to the PHP Chinese website!