引用如下:
import net.sf.json.JsonConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
综合来看,使用FastJson更为简便,仅供参考。
/**
* 将数据队列转换为JSon字符串
* 先处理成JSon数组,再转换为字符串
* @param coll 数据队列
* @return 返回JSon串
*
*/
public static String json(List<?> coll){
String result = null;
JsonConfig config = new JsonConfig();
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSONArray array =JSONArray.fromObject(coll, config);
result = array.toString();
return result;
}
/**
* 将Map转换为JSon字符串
* 先处理成JSon对象,再转换为字符串
* @param map 映射数据
* @return 返回JSon串
*
*/
public static String json(Map<?, ?> map){
String result = null;
JsonConfig config = new JsonConfig();
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSONObject object = JSONObject.fromObject(map, config);
result = object.toString();
return result;
}