您好,登錄后才能下訂單哦!
package com.cq2022.zago.base.util; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.util.List; import java.util.Map; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; import org.codehaus.jackson.map.annotate.JsonSerialize; import com.alibaba.fastjson.JSONArray; /** * Json工具類,實現JSON與Java Bean的互相轉換 */ public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); private static JsonFactory jsonFactory = new JsonFactory(); static { objectMapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false); objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); } /** * 泛型返回,json字符串轉對象 * @param jsonAsString * @param pojoClass * @return * @throws JsonMappingException * @throws JsonParseException * @throws IOException */ public static <T> T fromJson(String jsonAsString, Class<T> pojoClass) throws JsonMappingException, JsonParseException, IOException { return objectMapper.readValue(jsonAsString, pojoClass); } public static <T> T fromJson(FileReader fr, Class<T> pojoClass) throws JsonParseException, IOException { return objectMapper.readValue(fr, pojoClass); } /** * Object對象轉json * @param pojo * @return * @throws JsonMappingException * @throws JsonGenerationException * @throws IOException */ public static String toJson(Object pojo) throws JsonMappingException, JsonGenerationException, IOException { return toJson(pojo, false); } public static String toJson(Object pojo, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { StringWriter sw = new StringWriter(); JsonGenerator jg = jsonFactory.createJsonGenerator(sw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } objectMapper.writeValue(jg, pojo); return sw.toString(); } public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { JsonGenerator jg = jsonFactory.createJsonGenerator(fw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } objectMapper.writeValue(jg, pojo); } /** * json字符串轉Map * @param jsonStr * @return * @throws IOException */ public static Map<String, Object> parseMap(String jsonStr) throws IOException { Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class); return map; } public static JsonNode parse(String jsonStr) throws IOException { JsonNode node = null; node = objectMapper.readTree(jsonStr); return node; } public static ObjectMapper getObjectMapper() { return objectMapper; } /** * json字符串轉 List對象 * @param str json字符串 * @param clazz 轉換的類型 * @return */ public static <T> List<T> fromListJson(String str,Class<T> clazz){ return JSONArray.parseArray(str, clazz); } }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。如果你想了解更多相關內容請查看下面相關鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。