涉及到的jar包主要是gson-2.0.jar(必須),log4j.jar(可選)
使用方法
1,對象轉字元串:String str = JsonUtils.toJson(A, false)//第一個參數具體對象,第二個正常情況下一定要設為false
轉回對象A: Java代碼

- A a = JsonUtils.fromJson(str , A.calss)
,第一個是通過json轉的string,第二個參數目标對象
注意:如果是把List對象轉String,而List裡面的對象中又包含了其他對象,在取出的時候不能用foreach循環取出,
請采用如下方式
Java代碼

- List roList = JsonUtils.fromJson(str,List.class); //str是List轉為的string對象
- for (int i = 0; i < roList.size(); i++) {
- ResultObject ro = JsonUtils.fromJson(JsonUtils.toJson(roList.get(i), false),
- ResultObject.class);
- }
更多關于原生json内容請參考http://learning.iteye.com/blog/1289255
Java代碼

- import java.lang.reflect.Type;
- import java.util.Collection;
- import java.util.Enumeration;
- import java.util.Iterator;
- 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;
- public class JsonUtils {
- private static final Log log = LogFactory.getLog(JsonUtils.class);
- public static final String EMPTY = "";
- 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 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;
- }
- 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 (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;
- }
- }
- 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 (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;
- }
- }
- public static <T> T fromJson(String json, Class<T> clazz) {
- return fromJson(json, clazz, null);
- }
- public static boolean isEmpty(String inStr) {
- boolean reTag = false;
- if (inStr == null || "".equals(inStr)) {
- reTag = true;
- }
- return reTag;
- }
- }