JackJSON生成和解析JSON

1、导包
使用jackjson,将三个包导入到项目的lib目中(jar包已上传),截图所示:
在这里插入图片描述
2、JackJsonUtils生成json数据和解析json数据,代码:

public class JackJsonUtils {  
    static ObjectMapper objectMapper;  
    /** 
     * 解析json 
     *  
     * @param content 
     * @param valueType 
     * @return 
     */  
    public static <T> T fromJson(String content, Class<T> valueType) {  
        if (objectMapper == null) {  
            objectMapper = new ObjectMapper();  
        }  
        try {  
            return objectMapper.readValue(content, valueType);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
  
    /** 
     * 生成json 
     *  
     * @param object 
     * @return 
     */  
    public static String toJson(Object object) {  
        if (objectMapper == null) {  
            objectMapper = new ObjectMapper();  
        }  
        try {  
            return objectMapper.writeValueAsString(object);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
}  

猜你喜欢

转载自blog.csdn.net/sjn0815/article/details/92622177