JackJSON生成和解析JSON

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

public class JackJsonUtils {  
    static ObjectMapper objectMapper;  
    /** 
     * 解析json 
     *  
     * @param content 
     * @param valueType 
     * @return 
     */  
    public static  T fromJson(String content, Class 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;  
    }  
}  

你可能感兴趣的:(JackJSON生成和解析JSON)