这篇文章将为大家详细讲解有关Java如何实现操作JSON的便捷工具类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
具体如下:
对于JSON数据格式的处理,自开发Java以来,已用过多种JSON的开源工具,用得最好,也用得最High的恐怕要属Google的Gson了。
特别为它写了一个工具类,放入常备工具中,方便使用。下面是为GSON 1.5版本重写的工具类。
依赖包:
slf4j-api-1.6.0.jar
slf4j-log4j12-1.6.0.jar
log4j-1.2.15.jar
gson-1.5.jar
package my.tools;import java.lang.reflect.Type;import java.util.Collection;import java.util.Enumeration;import java.util.Iterator;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.reflect.TypeToken;import org.apache.commons.lang.StringUtils;public class JSONUtils { private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class); public static final String EMPTY_JSON = "{}"; public static final String EMPTY_JSON_ARRAY = "[]"; public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS"; public static final double SINCE_VERSION_10 = 1.0d; public static final double SINCE_VERSION_11 = 1.1d; public static final double SINCE_VERSION_12 = 1.2d; public static final double UNTIL_VERSION_10 = SINCE_VERSION_10; public static final double UNTIL_VERSION_11 = SINCE_VERSION_11; public static final double UNTIL_VERSION_12 = SINCE_VERSION_12; public JSONUtils() { super(); } public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version, String datePattern, boolean excludesFieldsWithoutExpose) { if (target == null) return EMPTY_JSON; GsonBuilder builder = new GsonBuilder(); if (isSerializeNulls) builder.serializeNulls(); if (version != null) builder.setVersion(version.doubleValue()); if (StringUtils.isBlank(datePattern)) datePattern = DEFAULT_DATE_PATTERN; builder.setDateFormat(datePattern); if (excludesFieldsWithoutExpose) builder.excludeFieldsWithoutExposeAnnotation(); return toJson(target, targetType, builder); } public static String toJson(Object target) { return toJson(target, null, false, null, null, true); } public static String toJson(Object target, String datePattern) { return toJson(target, null, false, null, datePattern, true); } public static String toJson(Object target, Double version) { return toJson(target, null, false, version, null, true); } public static String toJson(Object target, boolean excludesFieldsWithoutExpose) { return toJson(target, null, false, null, null, excludesFieldsWithoutExpose); } public static String toJson(Object target, Double version, boolean excludesFieldsWithoutExpose) { return toJson(target, null, false, version, null, excludesFieldsWithoutExpose); } public static String toJson(Object target, Type targetType) { return toJson(target, targetType, false, null, null, true); } public static String toJson(Object target, Type targetType, Double version) { return toJson(target, targetType, false, version, null, true); } public static String toJson(Object target, Type targetType, boolean excludesFieldsWithoutExpose) { return toJson(target, targetType, false, null, null, excludesFieldsWithoutExpose); } public static String toJson(Object target, Type targetType, Double version, boolean excludesFieldsWithoutExpose) { return toJson(target, targetType, false, version, null, excludesFieldsWithoutExpose); } public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) { if (StringUtils.isBlank(json)) { return null; } GsonBuilder builder = new GsonBuilder(); if (StringUtils.isBlank(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } Gson gson = builder.create(); try { return gson.fromJson(json, token.getType()); } catch (Exception ex) { LOGGER.error(json + " 无法转换为 " + token.getRawType().getName() + " 对象!", ex); return null; } } public static <T> T fromJson(String json, TypeToken<T> token) { return fromJson(json, token, null); } public static <T> T fromJson(String json, Class<T> clazz, String datePattern) { if (StringUtils.isBlank(json)) { return null; } GsonBuilder builder = new GsonBuilder(); if (StringUtils.isBlank(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } Gson gson = builder.create(); try { return gson.fromJson(json, clazz); } catch (Exception ex) { LOGGER.error(json + " 无法转换为 " + clazz.getName() + " 对象!", ex); return null; } } public static <T> T fromJson(String json, Class<T> clazz) { return fromJson(json, clazz, null); } public static String toJson(Object target, Type targetType, GsonBuilder builder) { if (target == null) return EMPTY_JSON; Gson gson = null; if (builder == null) { gson = new Gson(); } else { gson = builder.create(); } String result = EMPTY_JSON; try { if (targetType == null) { result = gson.toJson(target); } else { result = gson.toJson(target, targetType); } } catch (Exception ex) { LOGGER.warn("目标对象 " + target.getClass().getName() + " 转换 JSON 字符串时,发生异常!", ex); if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?> || target.getClass().isArray()) { result = EMPTY_JSON_ARRAY; } } return result; }}
关于“Java如何实现操作JSON的便捷工具类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。