第一种:

需要导入的jar包:

 

//调用Service层查询数据字典
List<BaseDict> list=baseDictService.getListByTypeCode(dict_code_type);
//将list 转化为jsong格式
String json = JSONArray.fromObject(list).toString();
//输入到前台
ServletActionContext.getResponse().setContentType("application/json;charset=utf8");
ServletActionContext.getResponse().getWriter().write(json);
return null;

 

第二种:

需要导入的jar包:GSON(谷歌)

List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);

response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);