Java GsonUtil帮助类

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




GsonUtil.java

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GsonUtil {
	
	/**
	 * ResultSet转json
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 * @throws JSONException
	 */
	public static String resultSetToJson(ResultSet rs) throws SQLException {
		// json数组
		JsonArray array = new JsonArray();
		// 获取列数
		ResultSetMetaData metaData = rs.getMetaData();
		int columnCount = metaData.getColumnCount();

		// 遍历ResultSet中的每条数据
		while (rs.next()) {
			JsonObject jsonObj = new JsonObject();
			for (int i = 1; i <= columnCount; i++) {
				String columnName = metaData.getColumnLabel(i);
				String value = rs.getString(columnName);
				jsonObj.addProperty(columnName, value);
			}
			array.add(jsonObj);
		}
		return array.toString();
	}
	
	/**
	 * 序列化
	 * 
	 * @param object
	 * @return
	 */
	public static String serialize(Object object) {
		Gson gson = new GsonBuilder().create();
		String jsonString = gson.toJson(object);
		return jsonString;
	}
	
	/**
	 * 反序列化
	 * 
	 * @param json
	 * @param object
	 * @return
	 */
	public static Object deserialize(String json, Object object) {
		Gson gson = new GsonBuilder().create();
		object = gson.fromJson(json, object.getClass());
		return object;
	}

	/**
	 * 将object对象转成json字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String GsonString(Object object) {
		//Gson gson = new GsonBuilder().create();
		Gson gson = new Gson();
		String gsonString = null;
		gsonString = gson.toJson(object);
		return gsonString;
	}
	
	/**
	 * 将List转成json字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String ListToGson(List list) {
		Gson gson = new Gson();
		String gsonString = gson.toJson(list);
		return gsonString;
	}

	/**
	 * 将gsonString转成泛型bean
	 * 
	 * @param gsonString
	 * @param cls
	 * @return
	 */
	public static <T> T GsonToBean(String gsonString, Class<T> cls) {
		/*Gson gson = null;*/
		T t = null;
		/*if (gson != null) {
			t = gson.fromJson(gsonString, cls);
		}*/
		return t;
	}

	/**
	 * 转成list 解决泛型问题
	 * 
	 * @param json
	 * @param cls
	 * @param      <T>
	 * @return
	 */
	public static <T> List<T> jsonToList(String json, Class<T> cls) {
		Gson gson = new Gson();
		List<T> list = new ArrayList<T>();
		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 <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
		/*Gson gson = null;*/
		List<Map<String, T>> list = null;
		/*if (gson != null) {
			list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
			}.getType());
		}*/
		return list;
	}


	/**
	 * ResultSet 转换为list
	 * 
	 * @param rs ResultSet
	 * @return list集合
	 */
	public static final List<Object> RsToJson(ResultSet rs) {
		JsonObject element = null;
		JsonArray ja = new JsonArray();
		ResultSetMetaData rsmd = null;
		String columnName, columnValue = null;
		List<Object> list = new ArrayList<Object>();
		try {
			rsmd = rs.getMetaData();
			while (rs.next()) {
				element = new JsonObject();
				for (int i = 0; i < rsmd.getColumnCount(); i++) {
					columnName = rsmd.getColumnName(i + 1);
					columnValue = rs.getString(columnName);
					element.addProperty(columnName, columnValue);
				}
				ja.add(element);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		for (JsonElement jsonElement : ja) {
			list.add(jsonElement);
		}
		return list;
	}
	
}

相关jar包下载

原文链接:https://www.idaobin.com/archives/2120.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

+ 60 = 68