json与java格式的相互转换
1: FastJson
maven配置:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.13</version> </dependency>
util:
package com.dw.study.util; /** * @Author dw * @ClassName FastJsonUtil * @Description * @Date 2020/5/7 22:48 * @Version 1.0 */ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; import java.util.List; import java.util.Map; /** * fastjson工具类 * @version:1.0.0 */ public class FastJsonUtil { private static final SerializeConfig config; static { config = new SerializeConfig(); config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 } private static final SerializerFeature[] features = { // 输出空置字段 SerializerFeature.WriteMapNullValue, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullListAsEmpty, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullNumberAsZero, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullBooleanAsFalse, // 字符类型字段如果为null,输出为"",而不是null SerializerFeature.WriteNullStringAsEmpty }; /** * 将对象转为json字符串 * * @param object * @return */ public static String objectToJsonByFeatures(Object object) { return JSON.toJSONString(object, config, features); } /** * 将对象转为json字符串 * * @param object * @return */ public static String objectToJson(Object object) { return JSON.toJSONString(object, config); } /** * 功能描述:把JSON数据转换成指定的java对象 * * @param jsonData JSON数据 * @param clazz 指定的java对象 * @return 指定的java对象 */ public static <T> T jsonToBean(String jsonData, Class<T> clazz) { return JSON.parseObject(jsonData, clazz); } /** * 功能描述:把JSON数据转换成指定的java对象列表 * * @param jsonData JSON数据 * @param clazz 指定的java对象 * @return List<T> */ public static <T> List<T> jsonToList(String jsonData, Class<T> clazz) { return JSON.parseArray(jsonData, clazz); } /** * List<T> 转 json */ public static <T> String listToJson(List<T> ts) { String jsons = JSON.toJSONString(ts); return jsons; } /** * 将json转为Map * * @param json * @param <T> * @return */ @SuppressWarnings("unchecked") public static <T> Map<String, T> toMap(String json) { return (Map<String, T>) JSONObject.parseObject(json); } /** * 功能描述:把JSON数据转换成较为复杂的List<Map<String, Object>> * * @param jsonData JSON数据 * @return List<Map < String, Object>> */ public static List<Map<String, Object>> jsonToListMap(String jsonData) { return JSON.parseObject(jsonData, new TypeReference<List<Map<String, Object>>>() { }); } }
2、Jackson
ObjectMapper的使用:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency>
Util:
/** * @Author dw * @ClassName JsonConvertUtil * @Description * @Date 2020/4/22 16:57 * @Version 1.0 */ public class JsonConvertUtil { public static ObjectMapper mapper = new ObjectMapper(); static { // 转换为格式化的json mapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果json中有新增的字段并且是实体类类中不存在的,不报错 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } /** * @return * @Author dw * @Description 对象转为String * @Date 2020/4/22 17:02 * @Param */ public String ObjectToString(Object object) throws JsonProcessingException { return mapper.writeValueAsString(object); } /** * @return * @Author dw * @Description 对象转byte数组 * @Date 2020/4/22 17:03 * @Param */ public byte[] ObjectToByte(Object object) throws JsonProcessingException { return mapper.writeValueAsBytes(object); } /** * @return * @Author dw * @Description 对象写入到文件中 * @Date 2020/4/22 17:04 * @Param */ public void ObjectToFile(String filePath, Object object) throws IOException { mapper.writeValue(new File(filePath), object); // mapper.writeValue(System.out, object); //写到控制台 } /** * @return * @Author dw * @Description 读取 Json String 到对象中 * @Date 2020/4/22 17:07 * @Param */ public <T> Object JsonStringToObject(String str, Class<T> tClass) throws JsonProcessingException { return mapper.readValue(str, tClass); } /** * @return * @Author dw * @Description Json Byte数组转为对象 * @Date 2020/4/22 17:10 * @Param */ public <T> Object byteArrToObject(byte[] byteArr, Class<T> tClass) throws IOException { return mapper.readValue(byteArr, tClass); } /** * @return * @Author dw * @Description list<Object> 转为JSon字符串 * @Date 2020/4/22 17:15 * @Param */ public <T> String listToString(List<T> list) throws JsonProcessingException { return mapper.writeValueAsString(list); } /** * @return * @Author dw * @Description JSON字符串转为List * @Date 2020/4/22 17:17 * @Param */ public <T> List<T> jsonToList(String jsonStr) throws JsonProcessingException { return mapper.readValue(jsonStr, List.class); } /** * @return * @Author dw * @Description Map 转Json字符串 * @Date 2020/4/22 17:21 * @Param */ public String mapToJSon(Map<Object, Object> map) throws JsonProcessingException { return mapper.writeValueAsString(map); } /** * @Author dw * @Description JSon 转 Map * @Date 2020/4/22 17:23 * @Param * @return */ public Map<Object, Object> jsonToMap(String jsonStr) throws JsonProcessingException { return mapper.readValue(jsonStr,Map.class); } }