package com.techson.himsnanhwa.admin.util; import java.io.IOException; //类1 public class JSONUtils { private static final Log log = LogFactory.getLog(JSONUtils.class); public static void sendJSON(HttpServletResponse response, String keywords, String values) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put(keywords, values); outResponse.print(jsonObject.toString()); outResponse.flush(); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } public static void sendJSON(HttpServletResponse response,JSONObject obj) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); outResponse.print(obj.toString()); outResponse.flush(); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } /** * JSONUtils 传输sendJSON的处理 * @param response servlet的响应 * @param error 结果为true error =0,结果为false error = 1; */ public static void sendJSON(HttpServletResponse response, int result) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put("result", result); outResponse.print(jsonObject.toString()); outResponse.flush(); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } public static void sendJSON(HttpServletResponse response, int error, JSONObject content) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put("error", error); jsonObject.put("content", content.get("msg")); outResponse.print(jsonObject.toString()); outResponse.flush(); log.info(jsonObject.toString()); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } /** * JSONUtils 传输sendJSON的处理 * @param response servlet的响应 * @param error error = 0 结果为true,error = 1结果为false; * @param message error=1对应的错误信息 * @param content error=0对应给request返回的内容 */ public static void sendJSON(HttpServletResponse response, int error, JSONArray content) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put("error", error); jsonObject.put("content", content); outResponse.print(jsonObject.toString()); outResponse.flush(); log.info(jsonObject.toString()); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } public static void sendJSON(HttpServletResponse response, String error, JSONArray countrycontent, JSONArray citycontent) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); JSONObject jsonObject = new JSONObject(); jsonObject.put("error", error); jsonObject.put("countryArr", countrycontent); jsonObject.put("cityArr", citycontent); outResponse.print(jsonObject.toString()); outResponse.flush(); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } public static void sendJSON(HttpServletResponse response,JSONArray array) { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); PrintWriter outResponse = null; try { outResponse = response.getWriter(); outResponse.print(array.toString()); outResponse.flush(); } catch (IOException e) { log.error("json utils exception :" + e); } finally { if(outResponse != null) { outResponse.close(); } } } public static boolean isExits(String key, JSONArray jsonArray, String type) { boolean result = false; for(int i=0; i< jsonArray.size(); i++) { JSONObject json = jsonArray.getJSONObject(i); if(json.get("areacode")!= null && json.get("areacode").equals(key) && "area".equalsIgnoreCase(type)) return true; if(json.get("citycode")!= null && json.get("citycode").equals(key) && "city".equalsIgnoreCase(type)) return true; } return result; } }
package com.zte.util; import java.io.StringWriter; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper; /** * JSON工具 * */ public class JSON { private static ObjectMapper mapper = new ObjectMapper(); /** * 将实体对象转换成JSON格式的字符串 * @param obj * @return */ public static String toJson(Object obj){ String json = ""; try { StringWriter writer = new StringWriter(); JsonGenerator generator = mapper.getJsonFactory().createJsonGenerator(writer); mapper.writeValue(generator, obj); json = writer.toString(); generator.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } return json; } /** * JSON格式的字符串转成实体对象 * @param json * @param valueType * @return */ public static <T> T fromJson(String json, Class<T> valueType){ try { return null == json ? null : mapper.readValue(json, valueType); } catch (Exception e) { e.printStackTrace(); return null; } } }
Map<String, Object> result = new HashMap<String, Object>(2); try { } catch (UploadFileException e) { result.put("code", 1); result.put("msg", e.getMessage()); writeResponse(response,result); return; } private void writeResponse(HttpServletResponse response, Map<String, Object> result) { try { response.setContentType("text/html;charset=utf-8"); response.getWriter().write(JSON.toJson(result)); response.getWriter().flush(); } catch (IOException e) { e.printStackTrace(); } }
另一个JSON工具类:
//Action: String s = this.versionService.saveOrUpdateVersion(version); if (MDSConstants.SUCCESS.equals(s)) { writeJson(new ResultJson(true, getText("保存成功"))); } else { writeJson(new ResultJson(true, getText("保存失败"))); } success : function(d) { var json = $.parseJSON(d); if (json.success) { $.messager.show({title : '系统提示',msg :json.msg}); }else{ $.messager.alert('系统提示',json.msg); } } 或: List<MngMmField> fields = this.tableService.getFieldsByTableId(oid,userId); if (fields != null && !fields.isEmpty()) { writeJosnArray(fields); } else { writeJosnArray(new ArrayList<MngMmField>()); } <script type="text/javascript"> // JSON 字符串 var strJSON = '{"Name":"Tom", "Age":14,"Enable":true}'; // var obj = jQuery.parseJSON(strJSON); alert( obj.Name ); </script>
//结果对象ResultJson.java: public class ResultJson implements Serializable { private boolean success = false; private String msg = null; private Integer resultcode = null; private Object result = null; public ResultJson() { } public ResultJson(boolean success) { setSuccess(success); } public ResultJson(boolean success, String msg, Object result) { setSuccess(success); this.msg = msg; this.result = result; } public ResultJson(boolean success, String msg) { setSuccess(success); this.msg = msg; } public ResultJson(boolean success, Object result) { setSuccess(success); this.result = result; } public String getMsg() { return this.msg; } public void setMsg(String msg) { this.msg = msg; } public void setResult(Object result) { this.result = result; } public Object getResult() { return this.result; } public void setSuccess(boolean success) { this.success = success; } public boolean isSuccess() { return this.success; } public Integer getResultcode() { return this.resultcode; } public void setResultcode(Integer resultcode) { this.resultcode = resultcode; } }
//JSON.util //输出方法 public void write(String str) { try { getResponse().setContentType("text/html;charset=utf-8"); PrintWriter writer = getResponse().getWriter(); writer.write(str); writer.flush(); writer.close(); } catch (Exception e) { } } public void writeJson(Object object, String dataFormatter) { if (dataFormatter == null) { dataFormatter = "yyyy-MM-dd"; } String json = JSON.toJSONStringWithDateFormat(object, dataFormatter, new SerializerFeature[0]); write(json); } public String getJson(Object object, String dataFormatter) { if (dataFormatter == null) { dataFormatter = "yyyy-MM-dd"; } return JSON.toJSONStringWithDateFormat(object, dataFormatter, new SerializerFeature[0]); } public void writeJson(Object object) { writeJson(object, null); } public void writeJsonObject(Object object) { JSONObject jsonObject = JSONObject.fromObject(object); write(jsonObject.toString()); } public void writeJosnArray(Collection collection) { JSONArray jsonArray = JSONArray.fromObject(collection); System.out.println("xxxd+" + jsonArray.toString()); write(jsonArray.toString()); }
。。。