欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实体转Json格式。把时间格式格式化

程序员文章站 2024-01-10 10:33:47
...

用自己的公用方法处理

把实体转json的同时,把时间格式化,且可以获取部分字段

// An highlighted block
// 时间格式
JsonDateValueProcessor jsonDate = new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss");
// 把实体转JSON (最后边null,如果想获取实体中的部分值可以写字段名 如: id,name,age )
JSONObject jsonObject = AbstractChangeJsonUtil.changeObjectToJSONObject(jsonDate,planSitePollDO,null);

下面是工具类

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import com.uncentury.hdq.infra.common.json.JsonFormatByName;

/**
 * ClassName: ActivityChangeJsonUtil
 * 
 * @Description: 这个工具类负责每个模块下面把实体转成json返回(AbstractChangeJsonUtil是抽象方法)
 * @author air
 * @date 2016-4-29
 */

public class AbstractChangeJsonUtil {

	/*---------------------这两个方法是抽象出来,专门拿来pick的,如果需要重命名的话,到时候在写 ------------*/
	public static <T> JSONArray changeListToJSONArray(List<T> list,
			String... pickName) {
		if (list == null || list.size() == 0) {
			return new JSONArray();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(list.get(0)
				.getClass(), null, pickName);
		JSONArray listJsonArray = JSONArray.fromObject(list, JsonFormatByName);
		return listJsonArray;
	}

	public static <T> JSONObject changeObjectToJSONObject(T object,
			String... pickName) {
		if (object == null) {
			return new JSONObject();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(object.getClass(),
				null, pickName);
		JSONObject objectJsonObject = JSONObject.fromObject(object,
				JsonFormatByName);
		return objectJsonObject;
	}

	/*---------------------这个方法支持JsonValueProcessor对象注入 ------------*/
	public static <T> JSONArray changeListToJSONArray(
			JsonValueProcessor jsonDateValueProcessor, List<T> list,
			String... pickName) {
		if (list == null || list.size() == 0) {
			return new JSONArray();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(
				jsonDateValueProcessor, list.get(0).getClass(), null, pickName);
		JSONArray listJsonArray = JSONArray.fromObject(list, JsonFormatByName);
		return listJsonArray;
	}

	public static <T> JSONObject changeObjectToJSONObject(
			JsonValueProcessor jsonDateValueProcessor, T object,
			String... pickName) {
		if (object == null) {
			return new JSONObject();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(
				jsonDateValueProcessor, object.getClass(), null, pickName);
		JSONObject objectJsonObject = JSONObject.fromObject(object,
				JsonFormatByName);
		return objectJsonObject;
	}

	// -----------------------分割线----------------------//

	public static <T> JSONArray changeListToJSONArray(List<T> list,
			Map<String, String> changeName, String... pickName) {
		if (list == null || list.size() == 0) {
			return new JSONArray();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(list.get(0)
				.getClass(), changeName, pickName);
		JSONArray listJsonArray = JSONArray.fromObject(list, JsonFormatByName);
		return listJsonArray;
	}

	public static <T> JSONObject changeObjectToJSONObject(T object,
			Map<String, String> changeName, String... pickName) {
		if (object == null) {
			return new JSONObject();
		}
		JsonConfig JsonFormatByName = new JsonFormatByName(object.getClass(),
				changeName, pickName);
		JSONObject objectJsonObject = JSONObject.fromObject(object,
				JsonFormatByName);
		return objectJsonObject;
	}

}
相关标签: Json处理