- import ?java.util.Iterator; ??
- import ?java.util.Collection; ??
- import ?java.util.Enumeration; ??
- import ?java.lang.reflect.Type; ??
- import ?org.apache.commons.logging.Log; ??
- import ?org.apache.commons.logging.LogFactory; ??
- import ?com.google.gson.Gson; ??
- import ?com.google.gson.GsonBuilder; ??
- import ?com.google.gson.reflect.TypeToken; ??
- /** ?
- ?*?包含操作?{@code?JSON}?數據的常用方法的工具類。 ?
- ?*?<p> ?
- ?*?該工具類使用的?{@code?JSON}?轉換引擎是?<a? ?
- ?*?target="_blank">{@code?Google?Gson}</a>。下面是工具類的使用案例: ?
- ?*?</p> ?
- ?*? ?
- ?*?<pre> ?
- ?*?public?class?User?{ ?
- ?*?????{@literal?@SerializedName("pwd")} ?
- ?*?????private?String?password; ?
- ?*?????{@literal?@Expose} ?
- ?*?????{@literal?@SerializedName("uname")} ?
- ?*?????private?String?username; ?
- ?*?????{@literal?@Expose} ?
- ?*?????{@literal?@Since(1.1)} ?
- ?*?????private?String?gender; ?
- ?*?????{@literal?@Expose} ?
- ?*?????{@literal?@Since(1.0)} ?
- ?*?????private?String?sex; ?
- ?*????? ?
- ?*?????public?User()?{} ?
- ?*?????public?User(String?username,?String?password,?String?gender)?{ ?
- ?*?????????//?user?constructor?code...?...?... ?
- ?*?????} ?
- ?*????? ?
- ?*?????public?String?getUsername() ?
- ?*?????...?...?... ?
- ?*?} ?
- ?*?List<User>?userList?=?new?LinkedList<User>(); ?
- ?*?User?jack?=?new?User("Jack",?"123456",?"Male"); ?
- ?*?User?marry?=?new?User("Marry",?"888888",?"Female"); ?
- ?*?userList.add(jack); ?
- ?*?userList.add(marry); ?
- ?*?Type?targetType?=?new?TypeToken<List<User>>(){}.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"}] ?
- ?*?</pre> ?
- ?*? ?
- ?*?@author?Fuchun ?
- ?*?@version?1.0,?2009-6-27 ?
- ?*/ ??
- public ? class ?JSONUtils? extends ?Utils?{ ??
- ???? @SuppressWarnings ( "unused" ) ??
- ???? private ? static ? final ?Log?log?=?LogFactory.getLog(JSONUtils. class ); ??
- ???? /**?空的?{@code?JSON}?數據?-?<code>"{}"</code>。?*/ ??
- ???? 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}?的?{@literal?@Since}?注解常用的版本號常量?-?{@code?1.0}。?*/ ??
- ???? public ? static ? final ?Double?SINCE_VERSION_10?=? 1 .0d; ??
- ???? /**?{@code?Google?Gson}?的?{@literal?@Since}?注解常用的版本號常量?-?{@code?1.1}。?*/ ??
- ???? public ? static ? final ?Double?SINCE_VERSION_11?=? 1 .1d; ??
- ???? /**?{@code?Google?Gson}?的?{@literal?@Since}?注解常用的版本號常量?-?{@code?1.2}。?*/ ??
- ???? public ? static ? final ?Double?SINCE_VERSION_12?=? 1 .2d; ??
- ???? /** ?
- ?????*?將給定的目標對象根據指定的條件參數轉換成?{@code?JSON}?格式的字符串。 ?
- ?????*?<p?/> ?
- ?????*?<strong>該方法轉換發生錯誤時,不會拋出任何異常。若發生錯誤時,曾通對象返回?<code>"{}"</code>; ?
- ?????*?集合或數組對象返回?<code>"[]"</code></strong> ?
- ?????*? ?
- ?????*?@param?target?目標對象。 ?
- ?????*?@param?targetType?目標對象的類型。 ?
- ?????*?@param?isSerializeNulls?是否序列化?{@code?null}?值字段。 ?
- ?????*?@param?version?字段的版本號注解。 ?
- ?????*?@param?datePattern?日期字段的格式化模式。 ?
- ?????*?@param?excludesFieldsWithoutExpose?是否排除未標注?{@literal?@Expose}?注解的字段。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? 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 ?(isEmpty(datePattern)) ??
- ????????????datePattern?=?DEFAULT_DATE_PATTERN; ??
- ????????builder.setDateFormat(datePattern); ??
- ???????? if ?(excludesFieldsWithoutExpose) ??
- ????????????builder.excludeFieldsWithoutExposeAnnotation(); ??
- ????????String?result?=?EMPTY; ??
- ????????Gson?gson?=?builder.create(); ??
- ???????? try ?{ ??
- ???????????? if ?(targetType?!=? null )?{ ??
- ????????????????result?=?gson.toJson(target,?targetType); ??
- ????????????}? else ?{ ??
- ????????????????result?=?gson.toJson(target); ??
- ????????????} ??
- ????????}? catch ?(Exception?ex)?{ ??
- ????????????log.warn( "目標對象?" ?+?target.getClass().getName()?+? "?轉換?JSON?字符串時,發生異常!" ,?ex); ??
- ???????????? if ?(target? instanceof ?Collection?||?target? instanceof ?Iterator ??
- ????????????????????||?target? instanceof ?Enumeration?||?target.getClass().isArray())?{ ??
- ????????????????result?=?EMPTY_JSON_ARRAY; ??
- ????????????}? else ??
- ????????????????result?=?EMPTY_JSON; ??
- ????????} ??
- ???????? return ?result; ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法只用來轉換普通的?{@code?JavaBean}?對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法只會轉換標有?{@literal?@Expose}?注解的字段;</li> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法會轉換所有未標注或已標注?{@literal?@Since}?的字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target)?{ ??
- ???????? return ?toJson(target,? null ,? false ,? null ,? null ,? true ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法只用來轉換普通的?{@code?JavaBean}?對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法只會轉換標有?{@literal?@Expose}?注解的字段;</li> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法會轉換所有未標注或已標注?{@literal?@Since}?的字段;</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?datePattern?日期字段的格式化模式。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?String?datePattern)?{ ??
- ???????? return ?toJson(target,? null ,? false ,? null ,?datePattern,? true ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法只用來轉換普通的?{@code?JavaBean}?對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法只會轉換標有?{@literal?@Expose}?注解的字段;</li> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?version?字段的版本號注解({@literal?@Since})。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Double?version)?{ ??
- ???????? return ?toJson(target,? null ,? false ,?version,? null ,? true ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法只用來轉換普通的?{@code?JavaBean}?對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法會轉換所有未標注或已標注?{@literal?@Since}?的字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?excludesFieldsWithoutExpose?是否排除未標注?{@literal?@Expose}?注解的字段。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,? boolean ?excludesFieldsWithoutExpose)?{ ??
- ???????? return ?toJson(target,? null ,? false ,? null ,? null ,?excludesFieldsWithoutExpose); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法只用來轉換普通的?{@code?JavaBean}?對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?version?字段的版本號注解({@literal?@Since})。 ?
- ?????*?@param?excludesFieldsWithoutExpose?是否排除未標注?{@literal?@Expose}?注解的字段。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Double?version,? boolean ?excludesFieldsWithoutExpose)?{ ??
- ???????? return ?toJson(target,? null ,? false ,?version,? null ,?excludesFieldsWithoutExpose); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法通常用來轉換使用泛型的對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法只會轉換標有?{@literal?@Expose}?注解的字段;</li> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法會轉換所有未標注或已標注?{@literal?@Since}?的字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?targetType?目標對象的類型。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Type?targetType)?{ ??
- ???????? return ?toJson(target,?targetType,? false ,? null ,? null ,? true ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法通常用來轉換使用泛型的對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法只會轉換標有?{@literal?@Expose}?注解的字段;</li> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?targetType?目標對象的類型。 ?
- ?????*?@param?version?字段的版本號注解({@literal?@Since})。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Type?targetType,?Double?version)?{ ??
- ???????? return ?toJson(target,?targetType,? false ,?version,? null ,? true ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法通常用來轉換使用泛型的對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法會轉換所有未標注或已標注?{@literal?@Since}?的字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?targetType?目標對象的類型。 ?
- ?????*?@param?excludesFieldsWithoutExpose?是否排除未標注?{@literal?@Expose}?注解的字段。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Type?targetType,? boolean ?excludesFieldsWithoutExpose)?{ ??
- ???????? return ?toJson(target,?targetType,? false ,? null ,? null ,?excludesFieldsWithoutExpose); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的目標對象轉換成?{@code?JSON}?格式的字符串。<strong>此方法通常用來轉換使用泛型的對象。</strong> ?
- ?????*?<ul> ?
- ?????*?<li>該方法不會轉換?{@code?null}?值字段;</li> ?
- ?????*?<li>該方法轉換時使用默認的?日期/時間?格式化模式?-?{@code?yyyy-MM-dd?HH:mm:ss?SSS};</li> ?
- ?????*?</ul> ?
- ?????*? ?
- ?????*?@param?target?要轉換成?{@code?JSON}?的目標對象。 ?
- ?????*?@param?targetType?目標對象的類型。 ?
- ?????*?@param?version?字段的版本號注解({@literal?@Since})。 ?
- ?????*?@param?excludesFieldsWithoutExpose?是否排除未標注?{@literal?@Expose}?注解的字段。 ?
- ?????*?@return?目標對象的?{@code?JSON}?格式的字符串。 ?
- ?????*/ ??
- ???? public ? static ?String?toJson(Object?target,?Type?targetType,?Double?version, ??
- ???????????? boolean ?excludesFieldsWithoutExpose)?{ ??
- ???????? return ?toJson(target,?targetType,? false ,?version,? null ,?excludesFieldsWithoutExpose); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的?{@code?JSON}?字符串轉換成指定的類型對象。 ?
- ?????*? ?
- ?????*?@param?<T>?要轉換的目標類型。 ?
- ?????*?@param?json?給定的?{@code?JSON}?字符串。 ?
- ?????*?@param?token?{@code?com.google.gson.reflect.TypeToken}?的類型指示類對象。 ?
- ?????*?@param?datePattern?日期格式模式。 ?
- ?????*?@return?給定的?{@code?JSON}?字符串表示的指定的類型對象。 ?
- ?????*/ ??
- ???? public ? static ?<T>?T?fromJson(String?json,?TypeToken<T>?token,?String?datePattern)?{ ??
- ???????? if ?(isEmpty(json))?{ ??
- ???????????? return ? null ; ??
- ????????} ??
- ????????GsonBuilder?builder?=? new ?GsonBuilder(); ??
- ???????? if ?(isEmpty(datePattern))?{ ??
- ????????????datePattern?=?DEFAULT_DATE_PATTERN; ??
- ????????} ??
- ????????Gson?gson?=?builder.create(); ??
- ???????? try ?{ ??
- ???????????? return ?gson.fromJson(json,?token.getType()); ??
- ????????}? catch ?(Exception?ex)?{ ??
- ????????????log.error(json?+? "?無法轉換為?" ?+?token.getRawType().getName()?+? "?對象!" ,?ex); ??
- ???????????? return ? null ; ??
- ????????} ??
- ????} ??
- ???? /** ?
- ?????*?將給定的?{@code?JSON}?字符串轉換成指定的類型對象。 ?
- ?????*? ?
- ?????*?@param?<T>?要轉換的目標類型。 ?
- ?????*?@param?json?給定的?{@code?JSON}?字符串。 ?
- ?????*?@param?token?{@code?com.google.gson.reflect.TypeToken}?的類型指示類對象。 ?
- ?????*?@return?給定的?{@code?JSON}?字符串表示的指定的類型對象。 ?
- ?????*/ ??
- ???? public ? static ?<T>?T?fromJson(String?json,?TypeToken<T>?token)?{ ??
- ???????? return ?fromJson(json,?token,? null ); ??
- ????} ??
- ???? /** ?
- ?????*?將給定的?{@code?JSON}?字符串轉換成指定的類型對象。<strong>此方法通常用來轉換普通的?{@code?JavaBean} ?
- ?????*?對象。</strong> ?
- ?????*? ?
- ?????*?@param?<T>?要轉換的目標類型。 ?
- ?????*?@param?json?給定的?{@code?JSON}?字符串。 ?
- ?????*?@param?clazz?要轉換的目標類。 ?
- ?????*?@param?datePattern?日期格式模式。 ?
- ?????*?@return?給定的?{@code?JSON}?字符串表示的指定的類型對象。 ?
- ?????*/ ??
- ???? public ? static ?<T>?T?fromJson(String?json,?Class<T>?clazz,?String?datePattern)?{ ??
- ???????? if ?(isEmpty(json))?{ ??
- ???????????? return ? null ; ??
- ????????} ??
- ????????GsonBuilder?builder?=? new ?GsonBuilder(); ??
- ???????? if ?(isEmpty(datePattern))?{ ??
- ????????????datePattern?=?DEFAULT_DATE_PATTERN; ??
- ????????} ??
- ????????Gson?gson?=?builder.create(); ??
- ???????? try ?{ ??
- ???????????? return ?gson.fromJson(json,?clazz); ??
- ????????}? catch ?(Exception?ex)?{ ??
- ????????????log.error(json?+? "?無法轉換為?" ?+?clazz.getName()?+? "?對象!" ,?ex); ??
- ???????????? return ? null ; ??
- ????????} ??
- ????} ??
- ???? /** ?
- ?????*?將給定的?{@code?JSON}?字符串轉換成指定的類型對象。<strong>此方法通常用來轉換普通的?{@code?JavaBean} ?
- ?????*?對象。</strong> ?
- ?????*? ?
- ?????*?@param?<T>?要轉換的目標類型。 ?
- ?????*?@param?json?給定的?{@code?JSON}?字符串。 ?
- ?????*?@param?clazz?要轉換的目標類。 ?
- ?????*?@return?給定的?{@code?JSON}?字符串表示的指定的類型對象。 ?
- ?????*/ ??
- ???? public ? static ?<T>?T?fromJson(String?json,?Class<T>?clazz)?{ ??
- ???????? return ?fromJson(json,?clazz,? null ); ??
- ????} ??
- }??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
