一个通用的java集合和json互转的工具类


基于java和json开发项目,经常需要将json串转换为java集合对象或者反过来转换,那么下面的一个工具类就挺有用的。



package com.tr.geda.evm.action.util;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;


public class JsonConverter {

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param list
	 * @return
	 * @throws JSONException
	 * @return JSONArray
	 * @throws 
	 */
	public static JSONArray listOfEntityToJsonArray(List list) throws JSONException {
		JSONArray array = new JSONArray();
		Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).setDateFormat(
				"yyyy-MM-dd HH:mm:ss").create();
		for (Object object : list ) {
			String jsonStr = gson.toJson(object);
			JSONObject json = new JSONObject(jsonStr);
			array.put(json);
		}
		return array;
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param map
	 * @return
	 * @throws JSONException
	 * @return JSONObject
	 * @throws 
	 */
	public static JSONObject mapToJson(Map<String, Object> map) throws JSONException {

		Type mapType = new TypeToken<Map<String, Object>>() {
		}.getType();
		Gson gson = new GsonBuilder().create();
		String jsonStr = gson.toJson(map, mapType);
		JSONObject json = new JSONObject(jsonStr);
		for (String key : map.keySet() ) {
			Object value = map.get(key);
			if (value==null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Date) {
				continue;
			} else if (value instanceof Map) {
				json.put(key, mapToJson((Map<String, Object>) map.get(key)));
			} else if (value instanceof List) {
				List list = (List) map.get(key);
				if (list.size() > 0) {
					if (list.get(0) instanceof Map)
						json.put(key, listOfMapToJsonArray((List<Map<String, Object>>) list));
					else
						json.put(key, listOfEntityToJsonArray(list));
				}
			} else {
				json.put(key, entityToJson(map.get(key)));
			}
		}
		return json;
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param json
	 * @return
	 * @return Map<String,String>
	 * @throws 
	 */
	public static Map<String, String> jsonToMap(JSONObject json) {
		Type mapType = new TypeToken<Map<String, String>>() {
		}.getType();
		Gson gson = new GsonBuilder().create();
		Map<String, String> map = gson.fromJson(json.toString(), mapType);
		return map;
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param array
	 * @return
	 * @throws JsonParseException
	 * @throws JSONException
	 * @return List<Map<String,String>>
	 * @throws 
	 */
	public static List<Map<String, String>> jsonArrayToListOfMap(JSONArray array) throws JsonParseException,
			JSONException {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Gson gson = new GsonBuilder().create();
		Type mapType = new TypeToken<Map<String, String>>() {
		}.getType();
		for (int i = 0; i < array.length(); i++ ) {
			Map<String, String> map = gson.fromJson(array.get(i).toString(), mapType);
			list.add(map);
		}
		return list;
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param list
	 * @return
	 * @throws JSONException
	 * @return JSONArray
	 * @throws 
	 */
	public static JSONArray listOfMapToJsonArray(List<Map<String, Object>> list) throws JSONException {
		JSONArray array = new JSONArray();
		Gson gson = new GsonBuilder().create();
		Type mapType = new TypeToken<Map<String, String>>() {
		}.getType();
		for (Map<String, Object> map : list ) {
			array.put(mapToJson(map));
		}
		return array;
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param <T>
	 * @param json
	 * @param classOfT
	 * @return
	 * @return T
	 * @throws 
	 */
	public static <T> T jsonToEntity(String json, Class<T> classOfT) {
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(json, classOfT);
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param object
	 * @return
	 * @throws JSONException
	 * @return JSONObject
	 * @throws 
	 */
	public static JSONObject entityToJson(Object object) throws JSONException {
		Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).setDateFormat(
				"yyyy-MM-dd HH:mm:ss").create();
		return new JSONObject(gson.toJson(object));
	}

	/**
	 * <p>Title: </p> 
	 * <p>Description: TODO</p> 
	 * 
	 * @param <T>
	 * @param array
	 * @param classOfT
	 * @return
	 * @throws JSONException
	 * @return List<T>
	 * @throws 
	 */
	public static <T> List<T> jsonArrayToEntityList(JSONArray array, Class<T> classOfT) throws JSONException {
		List<T> list = new ArrayList<T>();
		for (int i = 0; i < array.length(); i++ ) {
			list.add(jsonToEntity(array.getString(i), classOfT));
		}
		return list;
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值