JSONUtil.java 865 Bytes
package com.sitech.ismp.check.util;

import java.util.Collection;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author liujhc
 */
public class JSONUtil {

	/**
	 * 将java对象转换成json字符串
	 * @param obj
	 * @return
	 */
	public static String toJSON(Object obj) {
		if (obj == null) {
			return null;
		} else if (obj.getClass().isArray() || obj instanceof Collection) {
			return JSONArray.fromObject(obj).toString();
		} else {
			return JSONObject.fromObject(obj).toString();
		}
	}

	/**
	 * 将json字符串数组转换成java的List对象,对象转换成java的Map对象
	 * @param json
	 * @return
	 */
	public static Object fromJSON(String json) {
		if (json == null) {
			return null;
		} else if (json.startsWith("[")) {
			return JSONArray.fromObject(json);
		} else {
			return JSONObject.fromObject(json);
		}
	}
}