package com.common.bssp.common.util;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtil {
public static String toJson(Object obj) {
return JSONObject.fromObject(obj).toString();
}
public static String toJson(List list) {
return JSONArray.fromObject(list).toString();
}
public static <T> T toBean(String json, Class<T> cla) {
JSONObject jSONObject = JSONObject.fromObject(json);
return (T) JSONObject.toBean(jSONObject, cla);
}
public static List toList(String json, Class cla) {
JSONArray jsonArray = JSONArray.fromObject(json);
return JSONArray.toList(jsonArray, cla);
}
public static void main(String[] args) throws Exception {
// String jsonObj = "{\"pubContent\":\"123\"}";
// TaXportalWidgetExpPub item = toBean(jsonObj,
// TaXportalWidgetExpPub.class);
// System.out.println("toBean:" + item.getPubContent());
//
// String jsonArray = "[{\"pubContent\":\"123\"}]";
// List<TaXportalWidgetExpPub> list1 = toList(jsonArray,
// TaXportalWidgetExpPub.class);
// for (TaXportalWidgetExpPub item1 : list1) {
// System.out.println("toList:" + item1.getPubContent());
// }
//
// TaXportalWidgetExpPub item2 = new TaXportalWidgetExpPub();
// System.out.println("toJson:" + toJson(item2));
//
// List list = new ArrayList();
// list.add(item2);
// System.out.println("toJson:" + toJson(list));
Date date = new Date();
// System.out.println("toJson:" + objectToJson(date));
}
}