net.sf.json使用

来源:互联网 发布:清宫表和49算法哪个准 编辑:程序博客网 时间:2024/06/05 00:26
/** * 将指定Java对象转为json,并响应到客户端页面 * @param o * @param exclueds */public void java2Json(Object o ,String[] exclueds){JsonConfig jsonConfig = new JsonConfig();//指定哪些属性不需要转jsonjsonConfig.setExcludes(exclueds);String json = JSONObject.fromObject(o,jsonConfig).toString();ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");try {ServletActionContext.getResponse().getWriter().print(json);} catch (IOException e) {e.printStackTrace();}}/** * 将指定Java对象转为json,并响应到客户端页面 * @param o * @param exclueds */public void java2Json(List o ,String[] exclueds){JsonConfig jsonConfig = new JsonConfig();//指定哪些属性不需要转jsonjsonConfig.setExcludes(exclueds);String json = JSONArray.fromObject(o,jsonConfig).toString();ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");try {ServletActionContext.getResponse().getWriter().print(json);} catch (IOException e) {e.printStackTrace();}}