Java如何实现操作JSON的便捷工具类

这篇文章将为大家详细讲解有关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

/**
 * Copyright 2010 Fuchun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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;
/**
 * 包含操作 {@code JSON} 数据的常用方法的工具类。
 * 
 * 该工具类使用的 {@code JSON} 转换引擎是 
 * {@code Google Gson}。 下面是工具类的使用案例:
 *
 * 
 * public class User {
 *   @SerializedName("pwd")
 *   private String password;
 *   @Expose
 *   @SerializedName("uname")
 *   private String username;
 *   @Expose
 *   @Since(1.1)
 *   private String gender;
 *   @Expose
 *   @Since(1.0)
 *   private String sex;
 *
 *   public User() {}
 *   public User(String username, String password, String gender) {
 *     // user constructor code... ... ...
 *   }
 *
 *   public String getUsername()
 *   ... ... ...
 * }
 * List userList = new LinkedList();
 * User jack = new User("Jack", "123456", "Male");
 * User marry = new User("Marry", "888888", "Female");
 * userList.add(jack);
 * userList.add(marry);
 * Type targetType = new TypeToken>(){}.getType();
 * String sUserList1 = JSONUtils.toJson(userList, targetType);
 * sUserList1 ----> [{"uname":"jack","gender":"Male","sex":"Male"},{"uname":"marry","gender":"Female","sex":"Female"}]
 * String sUserList2 = JSONUtils.toJson(userList, targetType, false);
 * sUserList2 ----> [{"uname":"jack","pwd":"123456","gender":"Male","sex":"Male"},{"uname":"marry","pwd":"888888","gender":"Female","sex":"Female"}]
 * String sUserList3 = JSONUtils.toJson(userList, targetType, 1.0d, true);
 * sUserList3 ----> [{"uname":"jack","sex":"Male"},{"uname":"marry","sex":"Female"}]
 * 
 *  * @author Fuchun  * @since ay-commons-lang 1.0  * @version 1.1.0  */ public class JSONUtils {   private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class);   /** 空的 {@code JSON} 数据 - "{}"。 */   public static final String EMPTY_JSON = "{}";   /** 空的 {@code JSON} 数组(集合)数据 - {@code "[]"}。 */   public static final String EMPTY_JSON_ARRAY = "[]";   /** 默认的 {@code JSON} 日期/时间字段的格式化模式。 */   public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";   /** {@code Google Gson} 的 @Since 注解常用的版本号常量 - {@code 1.0}。 */   public static final double SINCE_VERSION_10 = 1.0d;   /** {@code Google Gson} 的 @Since 注解常用的版本号常量 - {@code 1.1}。 */   public static final double SINCE_VERSION_11 = 1.1d;   /** {@code Google Gson} 的 @Since 注解常用的版本号常量 - {@code 1.2}。 */   public static final double SINCE_VERSION_12 = 1.2d;   /** {@code Google Gson} 的 @Until 注解常用的版本号常量 - {@code 1.0}。 */   public static final double UNTIL_VERSION_10 = SINCE_VERSION_10;   /** {@code Google Gson} 的 @Until 注解常用的版本号常量 - {@code 1.1}。 */   public static final double UNTIL_VERSION_11 = SINCE_VERSION_11;   /** {@code Google Gson} 的 @Until 注解常用的版本号常量 - {@code 1.2}。 */   public static final double UNTIL_VERSION_12 = SINCE_VERSION_12;   /**    * 

   * JSONUtils instances should NOT be constructed in standard programming. Instead,    * the class should be used as JSONUtils.fromJson("foo");.    * 

   * 

   * This constructor is public to permit tools that require a JavaBean instance to operate.    * 

   */   public JSONUtils() {     super();   }   /**    * 将给定的目标对象根据指定的条件参数转换成 {@code JSON} 格式的字符串。    *     * 该方法转换发生错误时,不会抛出任何异常。若发生错误时,曾通对象返回 "{}"; 集合或数组对象返回 "[]"    *     *    * @param target 目标对象。    * @param targetType 目标对象的类型。    * @param isSerializeNulls 是否序列化 {@code null} 值字段。    * @param version 字段的版本号注解。    * @param datePattern 日期字段的格式化模式。    * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   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);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法只用来转换普通的 {@code JavaBean} 对象。    * 
       * 
  • 该方法只会转换标有 {@literal @Expose} 注解的字段;
  •    * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法会转换所有未标注或已标注 {@literal @Since} 的字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target) {     return toJson(target, null, false, null, null, true);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法只用来转换普通的 {@code JavaBean} 对象。    * 
       * 
  • 该方法只会转换标有 {@literal @Expose} 注解的字段;
  •    * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法会转换所有未标注或已标注 {@literal @Since} 的字段;
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param datePattern 日期字段的格式化模式。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, String datePattern) {     return toJson(target, null, false, null, datePattern, true);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法只用来转换普通的 {@code JavaBean} 对象。    * 
       * 
  • 该方法只会转换标有 {@literal @Expose} 注解的字段;
  •    * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param version 字段的版本号注解({@literal @Since})。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Double version) {     return toJson(target, null, false, version, null, true);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法只用来转换普通的 {@code JavaBean} 对象。    * 
       * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法会转换所有未标注或已标注 {@literal @Since} 的字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, boolean excludesFieldsWithoutExpose) {     return toJson(target, null, false, null, null, excludesFieldsWithoutExpose);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法只用来转换普通的 {@code JavaBean} 对象。    * 
       * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param version 字段的版本号注解({@literal @Since})。    * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Double version, boolean excludesFieldsWithoutExpose) {     return toJson(target, null, false, version, null, excludesFieldsWithoutExpose);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法通常用来转换使用泛型的对象。    * 
       * 
  • 该方法只会转换标有 {@literal @Expose} 注解的字段;
  •    * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法会转换所有未标注或已标注 {@literal @Since} 的字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param targetType 目标对象的类型。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Type targetType) {     return toJson(target, targetType, false, null, null, true);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法通常用来转换使用泛型的对象。    * 
       * 
  • 该方法只会转换标有 {@literal @Expose} 注解的字段;
  •    * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param targetType 目标对象的类型。    * @param version 字段的版本号注解({@literal @Since})。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Type targetType, Double version) {     return toJson(target, targetType, false, version, null, true);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法通常用来转换使用泛型的对象。    * 
       * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法会转换所有未标注或已标注 {@literal @Since} 的字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param targetType 目标对象的类型。    * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Type targetType, boolean excludesFieldsWithoutExpose) {     return toJson(target, targetType, false, null, null, excludesFieldsWithoutExpose);   }   /**    * 将给定的目标对象转换成 {@code JSON} 格式的字符串。此方法通常用来转换使用泛型的对象。    * 
       * 
  • 该方法不会转换 {@code null} 值字段;
  •    * 
  • 该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};
  •    * 
   *    * @param target 要转换成 {@code JSON} 的目标对象。    * @param targetType 目标对象的类型。    * @param version 字段的版本号注解({@literal @Since})。    * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.0    */   public static String toJson(Object target, Type targetType, Double version, boolean excludesFieldsWithoutExpose) {     return toJson(target, targetType, false, version, null, excludesFieldsWithoutExpose);   }   /**    * 将给定的 {@code JSON} 字符串转换成指定的类型对象。    *    * @param  要转换的目标类型。    * @param json 给定的 {@code JSON} 字符串。    * @param token {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。    * @param datePattern 日期格式模式。    * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。    * @since 1.0    */   public static  T fromJson(String json, TypeToken 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;     }   }   /**    * 将给定的 {@code JSON} 字符串转换成指定的类型对象。    *    * @param  要转换的目标类型。    * @param json 给定的 {@code JSON} 字符串。    * @param token {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。    * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。    * @since 1.0    */   public static  T fromJson(String json, TypeToken token) {     return fromJson(json, token, null);   }   /**    * 将给定的 {@code JSON} 字符串转换成指定的类型对象。此方法通常用来转换普通的 {@code JavaBean} 对象。    *    * @param  要转换的目标类型。    * @param json 给定的 {@code JSON} 字符串。    * @param clazz 要转换的目标类。    * @param datePattern 日期格式模式。    * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。    * @since 1.0    */   public static  T fromJson(String json, Class 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;     }   }   /**    * 将给定的 {@code JSON} 字符串转换成指定的类型对象。此方法通常用来转换普通的 {@code JavaBean} 对象。    *    * @param  要转换的目标类型。    * @param json 给定的 {@code JSON} 字符串。    * @param clazz 要转换的目标类。    * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。    * @since 1.0    */   public static  T fromJson(String json, Class clazz) {     return fromJson(json, clazz, null);   }   /**    * 将给定的目标对象根据{@code GsonBuilder} 所指定的条件参数转换成 {@code JSON} 格式的字符串。    *     * 该方法转换发生错误时,不会抛出任何异常。若发生错误时,{@code JavaBean} 对象返回 "{}"; 集合或数组对象返回    * "[]"。 其本基本类型,返回相应的基本值。    *    * @param target 目标对象。    * @param targetType 目标对象的类型。    * @param builder 可定制的{@code Gson} 构建器。    * @return 目标对象的 {@code JSON} 格式的字符串。    * @since 1.1    */   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的便捷工具类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


文章名称:Java如何实现操作JSON的便捷工具类
转载来源:http://lszwz.com/article/pseeip.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款