資源簡介
包含了gson-2.24和gson-2.85兩個版本,以及單獨使用所依賴的其他jar包,(log4j,slf4j,slf4j-api) , 附帶了 json轉bean, bean轉成json字符串的各種方法. 內直接由GsonUtil.java 類 可直接使用.

代碼片段和文件信息
package?shgjj.xxgx.util;
import?java.util.ArrayList;
import?java.util.HashMap;
import?java.util.List;
import?java.util.Map;
import?com.google.gson.Gson;
import?com.google.gson.JsonArray;
import?com.google.gson.JsonElement;
import?com.google.gson.JsonParser;
import?com.google.gson.reflect.TypeToken;
import?shgjj.xxgx.channels.fangdi.housecheck.message.HouseCheckParams;
import?shgjj.xxgx.channels.shuiwu.taxbill.message.BillTransParams;
import?shgjj.xxgx.common.constants.XxgxConst;
public?class?GsonUtil?{
private?static?Gson?gson?=?null;
static?{
if?(gson?==?null)?{
gson?=?new?Gson();
}
}
private?GsonUtil()?{
}
/**
?*?將object對象轉成json字符串
?*?
?*?@param?object
?*?@return
?*/
public?static?String?GsonString(object?object)?{
String?gsonString?=?null;
if?(gson?!=?null)?{
gsonString?=?gson.toJson(object);
}
return?gsonString;
}
/**
?*?將gsonString轉成泛型bean
?*?
?*?@param?gsonString
?*?@param?cls
?*?@return
?*/
public?static??T?GsonToBean(String?gsonString?Class?cls)?{
T?t?=?null;
if?(gson?!=?null)?{
t?=?gson.fromJson(gsonString?cls);
}
return?t;
}
/**
?*?轉成list?泛型在編譯期類型被擦除導致報錯
?*?
?*?@param?gsonString
?*?@param?cls
?*?@return
?*/
public?static??List?GsonToList(String?gsonString?Class?cls)?{
List?list?=?null;
if?(gson?!=?null)?{
list?=?gson.fromJson(gsonString?new?TypeToken>()?{
}.getType());
}
return?list;
}
/**
?*?轉成list?解決泛型問題
?*?
?*?@param?json
?*?@param?cls
?*?@param?
?*?@return
?*/
public?static??List?jsonToList(String?json?Class?cls)?{
Gson?gson?=?new?Gson();
List?list?=?new?ArrayList();
JsonArray?array?=?new?JsonParser().parse(json).getAsJsonArray();
for?(final?JsonElement?elem?:?array)?{
list.add(gson.fromJson(elem?cls));
}
return?list;
}
/**
?*?轉成list中有map的
?*?
?*?@param?gsonString
?*?@return
?*/
public?static??List
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????190432??2013-05-14?05:56??gson\gson\jars\gson-2.2.4.jar
?????文件?????241622??2018-05-22?11:08??gson\gson\jars\gson-2.8.5.jar
?????文件?????257430??2013-05-14?05:55??gson\gson\javadocs\gson-2.2.4-javadoc.jar
?????文件?????253187??2018-05-22?11:09??gson\gson\javadocs\gson-2.8.5-javadoc.jar
?????文件?????127562??2013-05-14?05:56??gson\gson\sources\gson-2.2.4-sources.jar
?????文件?????156280??2018-05-22?11:09??gson\gson\sources\gson-2.8.5-sources.jar
?????文件???????2957??2019-01-07?17:18??gson\GsonUtil.java
?????文件?????489884??2016-05-03?17:04??gson\log4j\bundles\log4j-1.2.17.jar
?????文件????1491397??2016-05-03?17:04??gson\log4j\javadocs\log4j-1.2.17-javadoc.jar
?????文件?????481200??2016-05-03?17:04??gson\log4j\sources\log4j-1.2.17-sources.jar
?????文件??????16516??2017-02-24?19:06??gson\org.slf4j\jcl-over-slf4j\jars\jcl-over-slf4j-1.7.24.jar
?????文件??????16515??2017-03-17?00:37??gson\org.slf4j\jcl-over-slf4j\jars\jcl-over-slf4j-1.7.25.jar
?????文件??????16519??2016-05-03?17:05??gson\org.slf4j\jcl-over-slf4j\jars\jcl-over-slf4j-1.7.7.jar
?????文件?????106172??2017-02-24?19:06??gson\org.slf4j\jcl-over-slf4j\javadocs\jcl-over-slf4j-1.7.24-javadoc.jar
?????文件?????106165??2017-03-17?00:37??gson\org.slf4j\jcl-over-slf4j\javadocs\jcl-over-slf4j-1.7.25-javadoc.jar
?????文件??????93244??2016-05-03?17:05??gson\org.slf4j\jcl-over-slf4j\javadocs\jcl-over-slf4j-1.7.7-javadoc.jar
?????文件??????23239??2017-02-24?19:06??gson\org.slf4j\jcl-over-slf4j\sources\jcl-over-slf4j-1.7.24-sources.jar
?????文件??????23239??2017-03-17?00:37??gson\org.slf4j\jcl-over-slf4j\sources\jcl-over-slf4j-1.7.25-sources.jar
?????文件??????23044??2016-05-03?17:05??gson\org.slf4j\jcl-over-slf4j\sources\jcl-over-slf4j-1.7.7-sources.jar
?????文件??????41077??2016-12-14?01:05??gson\org.slf4j\slf4j-api\jars\slf4j-api-1.7.22.jar
?????文件??????41205??2017-02-24?19:05??gson\org.slf4j\slf4j-api\jars\slf4j-api-1.7.24.jar
?????文件??????41203??2017-03-17?00:36??gson\org.slf4j\slf4j-api\jars\slf4j-api-1.7.25.jar
?????文件??????29257??2016-05-03?17:05??gson\org.slf4j\slf4j-api\jars\slf4j-api-1.7.7.jar
?????文件?????248685??2016-12-14?01:05??gson\org.slf4j\slf4j-api\javadocs\slf4j-api-1.7.22-javadoc.jar
?????文件?????248823??2017-02-24?19:05??gson\org.slf4j\slf4j-api\javadocs\slf4j-api-1.7.24-javadoc.jar
?????文件?????248922??2017-03-17?00:36??gson\org.slf4j\slf4j-api\javadocs\slf4j-api-1.7.25-javadoc.jar
?????文件?????195678??2016-05-03?17:05??gson\org.slf4j\slf4j-api\javadocs\slf4j-api-1.7.7-javadoc.jar
?????文件??????55650??2016-12-14?01:05??gson\org.slf4j\slf4j-api\sources\slf4j-api-1.7.22-sources.jar
?????文件??????55538??2017-02-24?19:05??gson\org.slf4j\slf4j-api\sources\slf4j-api-1.7.24-sources.jar
?????文件??????55539??2017-03-17?00:36??gson\org.slf4j\slf4j-api\sources\slf4j-api-1.7.25-sources.jar
............此處省略49個文件信息
評論
共有 條評論