Here you can find the source of toBean(Class
Parameter | Description |
---|---|
E | the type parameter |
clazz | the clazz |
json | the json |
Parameter | Description |
---|---|
IOException | the io exception |
public static <E> E toBean(Class<E> clazz, String json) throws IOException
//package com.java2s; //License from project: Apache License import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.StringWriter; import java.text.SimpleDateFormat; public class Main { /**/*from w w w.jav a2 s. c o m*/ * The constant DATE_FORMAT_DEF. */ public static String DATE_FORMAT_DEF = "yyyy-MM-dd HH:mm:ss"; /** * To bean e. * * @param <E> the type parameter * @param clazz the clazz * @param json the json * @return the e * @throws IOException the io exception */ public static <E> E toBean(Class<E> clazz, String json) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT_DEF)); objectMapper.getDeserializationConfig().with(new SimpleDateFormat(DATE_FORMAT_DEF)); StringWriter stringWriter = new StringWriter(); return objectMapper.readValue(json, clazz); } }