• Json工具类


    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.List;
    
    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    
    public class JsonMapper {
    
        private static final ObjectMapper jsonMapper;
        
        static {
        	jsonMapper = new ObjectMapper();
        	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        	jsonMapper.setDateFormat(df);
        	jsonMapper.setSerializationInclusion(Include.NON_NULL);
        	jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
        
        public static String bean2Json(Object value){
        	try {
    			return jsonMapper.writeValueAsString(value);
    		} catch (JsonProcessingException e) {
    			throw new RuntimeException(e);
    		}
        }
        
        public static <T> T json2Bean(String json,Class<T> clazz){
        	try {
    			return jsonMapper.readValue(json, clazz);
    		} catch (IOException e) {
    			throw new RuntimeException(e);
    		}
        }
        
        public static <T> T json2Array(String json,TypeReference<?> type){
        	try {
    			return jsonMapper.readValue(json, type);
    		} catch (IOException e) {
    			throw new RuntimeException(e);
    		}
        }
    
    	public static <T> List<T> json2List(String json, Class<T> clazz){
    		try {
    			JavaType type = jsonMapper.getTypeFactory().constructParametrizedType(List.class,List.class,clazz);
    			return jsonMapper.readValue(json, type);
    		} catch (IOException e) {
    			throw new RuntimeException(e);
    		}
    	}
    }
    

      

  • 相关阅读:
    【Vue前端】Vue前端注册业务实现!!!【代码】
    QQ第三方登录逻辑(微信,微博等同)
    发送短信验证码逻辑
    web图形验证码逻辑
    PID算法资料【视频+PDF介绍】
    如何配置电脑本地的域名
    js实现阻止默认事件preventDefault与returnValue
    js实现事件监听与阻止监听传播
    json字符串转换对象的方法1
    json字符串转换对象的方法
  • 原文地址:https://www.cnblogs.com/piaxiaohui/p/9227066.html
Copyright © 2020-2023  润新知