欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

将集合转换成json对象时,去除不想要的字段

程序员文章站 2022-03-03 12:32:30
...

 

public void selectAllType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			CatService cs=new CatService();
			List<Type> types=cs.getType();
			System.out.println("_____"+types.size());
			
            //通过JsonConfig的对象,来去除,json数据中不要的字段
			JsonConfig cfg = new JsonConfig();
			cfg.setExcludes(new String[]{"Cars","Car","type","Type"});
			 
			//将集合转换成json
			JSONArray ja=JSONArray.fromObject(types,cfg);
			
			//将json响应给异步
			PrintWriter out=response.getWriter();
			out.print(ja.toString());
			out.flush();
			out.close();
			

		}

 

相关标签: json