JAVA对象、集合转json


若使用net.sf.json
//对象转json
JSONObject userJson = JSONObject.fromObject(user);
//对象转json
JSONArray listJson = JSONArray.fromObject(userList);

若使用fastjson
//对象转json
JSONObject userJson = (JSONObject)JSONObject.toJSON(user);
//list转json
JSONArray listJson = (JSONArray)JSONArray.toJSON(userList);