import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
// list 转 json
List list //已经赋值
JsonConfig config = new JsonConfig();
//排除其他属性
config.setExcludes(new String[]{“grade”, “studentList”});
String result = JSONArray.fromObject(list, config).toString();
response.getWriter().write(result);
// list 转 json
List<Map<String, Object>> list
//格式化Map,以json格式返回数据
String result = JSONArray.fromObject(list).toString();
list / 数组 JSONArray.fromObject()
// map 转 json
//定义Map
Map<String, Object> jsonMap = new HashMap<String, Object>();
//total键 存放总记录数,必须的
jsonMap.put(“total”, total);
//rows键 存放每页记录 list
jsonMap.put(“rows”, list);
//格式化Map,以json格式返回数据
String result = JSONObject.fromObject(jsonMap).toString();
response.getWriter().write(result);
Bean / map JSONObject.fromObject()