|
寫在前面:開發(fā)中經(jīng)常用到j(luò)son和對象的相互轉(zhuǎn)換,下面將列出FastJson中ObjectMapper對象的API的使用
一、maven工程中pom導(dǎo)入 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency> 二、使用 1、創(chuàng)建對象 public static ObjectMapper mapper = new ObjectMapper(); 2、初始化 static { // 轉(zhuǎn)換為格式化的json mapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果json中有新增的字段并且是實(shí)體類類中不存在的,不報錯 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //修改日期格式 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); } 3、對象轉(zhuǎn)為字符串 String jsonStr = mapper.writeValueAsString(user); System.out.println("對象轉(zhuǎn)為字符串:" jsonStr); 4、對象轉(zhuǎn)為byte數(shù)組 byte[] byteArr = mapper.writeValueAsBytes(user); System.out.println("對象轉(zhuǎn)為byte數(shù)組:" byteArr); 5、json字符串轉(zhuǎn)為對象 ObjectClass obj = mapper.readValue(jsonStr, ObjectClass.class); System.out.println("json字符串轉(zhuǎn)為對象:" obj); 6、byte數(shù)組轉(zhuǎn)為對象 ObjectClass obj = mapper.readValue(byteArr,ObjectClass.class); System.out.println("byte數(shù)組轉(zhuǎn)為對象:" obj); 7、集合轉(zhuǎn)為字符串 String jsonStr = mapper.writeValueAsString(userList); System.out.println("集合轉(zhuǎn)為字符串:" jsonStr); 8、字符串轉(zhuǎn)集合 List list = null; try { list = mapper.readValue(jsonStr, List.class); } catch (IOException e1) { e1.printStackTrace(); } 9、Map轉(zhuǎn)為字符串 String jsonStr = mapper.writeValueAsString(testMap); System.out.println("Map轉(zhuǎn)為字符串:" jsonStr); 10、字符串轉(zhuǎn)Map Map map = null; try { map = mapper.readValue(jsonStr, Map.class); } catch (IOException e1) { e1.printStackTrace(); } 三、JsonUtils工具類 import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; /** * @Author Guixing * @Date 2019/1/7 11:10 * @Description */ public class JsonUtils { public static ObjectMapper mapper = new ObjectMapper(); static { // 轉(zhuǎn)換為格式化的json mapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果json中有新增的字段并且是實(shí)體類類中不存在的,不報錯 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //修改日期格式 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); } /** * 對象轉(zhuǎn)為字符串 * * @param obj * @return */ public static String Object2Json(Object obj) { String jsonStr = null; try { jsonStr = mapper.writeValueAsString(obj); } catch (JsonProcessingException e1) { e1.printStackTrace(); } return jsonStr; } /** * 對象轉(zhuǎn)為byte數(shù)組 * * @param obj * @return */ public static byte[] object2ByteArray(Object obj) { byte[] byteArr = new byte[0]; try { byteArr = mapper.writeValueAsBytes(obj); } catch (JsonProcessingException e1) { e1.printStackTrace(); } return byteArr; } /** * json字符串轉(zhuǎn)為對象 * * @param jsonStr * @param beanType * @param <T> * @return */ public static <T> T json2Object(String jsonStr, Class<T> beanType) { T t = null; try { t = mapper.readValue(jsonStr, beanType); } catch (IOException e1) { e1.printStackTrace(); } return t; } /** * byte數(shù)組轉(zhuǎn)為對象 * * @param byteArr * @param beanType * @param <T> * @return */ public static <T> T byteArr2Object(byte[] byteArr, Class<T> beanType) { T t = null; try { t = mapper.readValue(byteArr, beanType); } catch (Exception e) { e.printStackTrace(); } return t; } /** * 集合轉(zhuǎn)為字符串 * * @param list * @return */ public static String list2String(List list) { String jsonStr = null; try { jsonStr = mapper.writeValueAsString(list); } catch (JsonProcessingException e1) { e1.printStackTrace(); } return jsonStr; } /** * 字符串轉(zhuǎn)集合 * * @param jsonStr * @return */ public static List json2List(String jsonStr) { List list = null; try { list = mapper.readValue(jsonStr, List.class); } catch (IOException e1) { e1.printStackTrace(); } return list; } /** * Map轉(zhuǎn)為字符串 * * @param map * @return */ public static String map2String(Map map) { String jsonStr = null; try { jsonStr = mapper.writeValueAsString(map); } catch (JsonProcessingException e1) { e1.printStackTrace(); } return jsonStr; } /** * 字符串轉(zhuǎn)Map * * @param jsonStr * @return */ public static Map json2Map(String jsonStr) { Map map = null; try { map = mapper.readValue(jsonStr, Map.class); } catch (IOException e1) { e1.printStackTrace(); } return map; } }
———————————————— 版權(quán)聲明:本文為CSDN博主「興躍神話」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qq_37394874/article/details/85992380
來源:https://www./content-4-595851.html
|