From 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期六, 09 五月 2020 21:41:27 +0800
Subject: [PATCH] 2.1需求

---
 utils/src/main/java/org/yeshi/utils/JsonUtil.java |   73 +++++++++++++++++++++++++++++++++++-
 1 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/utils/src/main/java/org/yeshi/utils/JsonUtil.java b/utils/src/main/java/org/yeshi/utils/JsonUtil.java
index 730f16c..d05d734 100644
--- a/utils/src/main/java/org/yeshi/utils/JsonUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/JsonUtil.java
@@ -5,12 +5,13 @@
 import java.lang.reflect.Type;
 import java.math.BigDecimal;
 import java.text.DateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import com.google.gson.ExclusionStrategy;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
@@ -22,9 +23,13 @@
 import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonWriter;
 
+import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 
 public class JsonUtil {
+
+	static Gson baseGson = new Gson();
+
 	public static String loadTrueResult(Object result) {
 		JSONObject object = new JSONObject();
 		object.put("code", 0);
@@ -58,7 +63,10 @@
 	}
 
 	public static String loadJSONP(String callback, String data) {
-		return callback + "(" + data + ")";
+		if (StringUtil.isNullOrEmpty(callback))
+			return data;
+		else
+			return callback + "(" + data + ")";
 	}
 
 	public static String loadFalseResult(int code, String error) {
@@ -194,7 +202,8 @@
 						desc = (nowDay - oldDay) + "澶╁墠";
 					}
 					out.value(desc);
-				}
+				} else
+					out.value("");
 			}
 
 			@Override
@@ -214,4 +223,62 @@
 		return map;
 	}
 
+	/**
+	 * 灏嗗璞¤浆鎹㈡垚json瀛楃涓层��
+	 * <p>
+	 * Title: pojoToJson
+	 * </p>
+	 * <p>
+	 * Description:
+	 * </p>
+	 * 
+	 * @param data
+	 * @return
+	 */
+	public static String objectToJson(Object data) {
+		String string = baseGson.toJson(data);
+		return string;
+	}
+
+	/**
+	 * 灏唈son缁撴灉闆嗚浆鍖栦负瀵硅薄
+	 * 
+	 * @param jsonData
+	 *            json鏁版嵁
+	 * @param clazz
+	 *            瀵硅薄涓殑object绫诲瀷
+	 * @return
+	 */
+	public static <T> T jsonToObject(String jsonData, Class<T> beanType) {
+		return baseGson.fromJson(jsonData, beanType);
+	}
+
+	/**
+	 * 灏唈son鏁版嵁杞崲鎴恜ojo瀵硅薄list
+	 * <p>
+	 * Title: jsonToList
+	 * </p>
+	 * <p>
+	 * Description:
+	 * </p>
+	 * 
+	 * @param jsonData
+	 * @param beanType
+	 * @return
+	 */
+	public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
+		if (StringUtil.isNullOrEmpty(jsonData))
+			return null;
+		List<T> list = new ArrayList<>();
+		JSONArray array = JSONArray.fromObject(jsonData);
+		for (int i = 0; i < array.size(); i++) {
+			try {
+				list.add(baseGson.fromJson(array.optJSONObject(i).toString(), beanType));
+			} catch (Exception e) {
+				list.add(baseGson.fromJson(array.optString(i), beanType));
+			}
+		}
+		return list;
+	}
+
 }

--
Gitblit v1.8.0