Java Json operating entity class

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

/**
 * @author hhh
 * @date 2019/7/28 15:52
 * @Despriction
 */
public class JsonUtil {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     */
    public staticObjectToJson String (Object Data) {
         the try {
             return MAPPER.writeValueAsString (Data); 
        } the catch (JsonProcessingException E) { 
            e.printStackTrace (); 
        } 
        return  null ; 
    } 

    / ** 
     * The result sets into json object 
     * 
     * @param jsonData json data 
     * @param BeanType object type object
      * / 
    public  static <T> T jsonToPojo (String jsonData, Class <T> BeanType) {
         the try {
             return MAPPER.readValue(jsonData, beanType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        } 

        Return  null; 
    } 


    / ** 
     * Convert data into json pojo Object List 
     * / 
    public  static the Map jsonToMap (Object Data) {
         the try {
             return MAPPER.convertValue (Data, the Map. Class ); 
        } the catch (Exception E) { 
            e.printStackTrace ( ); 
        } 
        return  null ; 
    }

 

Guess you like

Origin www.cnblogs.com/huanghuanghui/p/11263696.html