Json工具类

来源:互联网 发布:div随窗口大小变化js 编辑:程序博客网 时间:2024/05/12 06:48
Java代码  收藏代码
  1. package com.techson.himsnanhwa.admin.util;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. //类1  
  6. public class JSONUtils {  
  7.       
  8.     private static final Log log = LogFactory.getLog(JSONUtils.class);  
  9.       
  10.     public static void sendJSON(HttpServletResponse response, String keywords, String values) {  
  11.         response.setContentType("application/json");  
  12.         response.setCharacterEncoding("utf-8");  
  13.         PrintWriter outResponse = null;  
  14.         try {  
  15.             outResponse = response.getWriter();  
  16.             JSONObject jsonObject = new JSONObject();  
  17.             jsonObject.put(keywords, values);  
  18.             outResponse.print(jsonObject.toString());  
  19.             outResponse.flush();  
  20.         } catch (IOException e) {  
  21.             log.error("json utils exception :" + e);  
  22.         } finally {  
  23.             if(outResponse != null) {  
  24.                 outResponse.close();  
  25.             }  
  26.         }  
  27.     }  
  28.       
  29.     public static void sendJSON(HttpServletResponse response,JSONObject obj) {  
  30.         response.setContentType("application/json");  
  31.         response.setCharacterEncoding("utf-8");  
  32.         PrintWriter outResponse = null;  
  33.           
  34.         try {  
  35.             outResponse = response.getWriter();  
  36.             outResponse.print(obj.toString());  
  37.             outResponse.flush();  
  38.         } catch (IOException e) {  
  39.             log.error("json utils exception :" + e);  
  40.         } finally {  
  41.             if(outResponse != null) {  
  42.                 outResponse.close();  
  43.             }  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      *  JSONUtils 传输sendJSON的处理 
  49.      * @param response servlet的响应 
  50.      * @param error 结果为true error =0,结果为false error = 1; 
  51.      */  
  52.     public static void sendJSON(HttpServletResponse response, int result) {  
  53.         response.setContentType("application/json");  
  54.         response.setCharacterEncoding("utf-8");  
  55.         PrintWriter outResponse = null;  
  56.         try {  
  57.             outResponse = response.getWriter();  
  58.             JSONObject jsonObject = new JSONObject();  
  59.             jsonObject.put("result", result);  
  60.             outResponse.print(jsonObject.toString());  
  61.             outResponse.flush();  
  62.         } catch (IOException e) {  
  63.             log.error("json utils exception :" + e);  
  64.         } finally {  
  65.             if(outResponse != null) {  
  66.                 outResponse.close();  
  67.             }  
  68.         }  
  69.     }  
  70.       
  71.     public static void sendJSON(HttpServletResponse response,  
  72.             int error, JSONObject content) {  
  73.         response.setContentType("application/json");  
  74.         response.setCharacterEncoding("utf-8");  
  75.         PrintWriter outResponse = null;  
  76.         try {  
  77.             outResponse = response.getWriter();  
  78.             JSONObject jsonObject = new JSONObject();  
  79.             jsonObject.put("error", error);  
  80.             jsonObject.put("content", content.get("msg"));  
  81.             outResponse.print(jsonObject.toString());  
  82.             outResponse.flush();  
  83.             log.info(jsonObject.toString());  
  84.         } catch (IOException e) {  
  85.             log.error("json utils exception :" + e);  
  86.         } finally {  
  87.             if(outResponse != null) {  
  88.                 outResponse.close();  
  89.             }  
  90.         }  
  91.     }  
  92.       
  93.       
  94.     /** 
  95.      * JSONUtils 传输sendJSON的处理 
  96.      * @param response servlet的响应 
  97.      * @param error error = 0 结果为true,error = 1结果为false; 
  98.      * @param message error=1对应的错误信息 
  99.      * @param content error=0对应给request返回的内容 
  100.      */  
  101.     public static void sendJSON(HttpServletResponse response,  
  102.             int error, JSONArray content) {  
  103.         response.setContentType("application/json");  
  104.         response.setCharacterEncoding("utf-8");  
  105.         PrintWriter outResponse = null;  
  106.         try {  
  107.             outResponse = response.getWriter();  
  108.             JSONObject jsonObject = new JSONObject();  
  109.             jsonObject.put("error", error);  
  110.             jsonObject.put("content", content);  
  111.             outResponse.print(jsonObject.toString());  
  112.             outResponse.flush();  
  113.             log.info(jsonObject.toString());  
  114.         } catch (IOException e) {  
  115.             log.error("json utils exception :" + e);  
  116.         } finally {  
  117.             if(outResponse != null) {  
  118.                 outResponse.close();  
  119.             }  
  120.         }  
  121.     }  
  122.       
  123.     public static void sendJSON(HttpServletResponse response,  
  124.             String error, JSONArray countrycontent, JSONArray citycontent) {  
  125.         response.setContentType("application/json");  
  126.         response.setCharacterEncoding("utf-8");  
  127.         PrintWriter outResponse = null;  
  128.         try {  
  129.             outResponse = response.getWriter();  
  130.             JSONObject jsonObject = new JSONObject();  
  131.             jsonObject.put("error", error);  
  132.             jsonObject.put("countryArr", countrycontent);  
  133.             jsonObject.put("cityArr", citycontent);  
  134.             outResponse.print(jsonObject.toString());  
  135.             outResponse.flush();  
  136.         } catch (IOException e) {  
  137.             log.error("json utils exception :" + e);  
  138.         } finally {  
  139.             if(outResponse != null) {  
  140.                 outResponse.close();  
  141.             }  
  142.         }  
  143.     }  
  144.       
  145.     public static void sendJSON(HttpServletResponse response,JSONArray array) {  
  146.         response.setContentType("application/json");  
  147.         response.setCharacterEncoding("utf-8");  
  148.         PrintWriter outResponse = null;  
  149.         try {  
  150.             outResponse = response.getWriter();  
  151.             outResponse.print(array.toString());  
  152.             outResponse.flush();  
  153.         } catch (IOException e) {  
  154.             log.error("json utils exception :" + e);  
  155.         } finally {  
  156.             if(outResponse != null) {  
  157.                 outResponse.close();  
  158.             }  
  159.         }  
  160.     }  
  161.       
  162.     public static boolean isExits(String key, JSONArray jsonArray, String type) {  
  163.         boolean result = false;  
  164.         for(int i=0; i< jsonArray.size(); i++) {  
  165.             JSONObject json = jsonArray.getJSONObject(i);  
  166.             if(json.get("areacode")!= null && json.get("areacode").equals(key) && "area".equalsIgnoreCase(type))  
  167.                 return true;  
  168.               
  169.             if(json.get("citycode")!= null && json.get("citycode").equals(key) && "city".equalsIgnoreCase(type))  
  170.                 return true;  
  171.         }  
  172.         return result;  
  173.     }  
  174. }  

 

Java代码  收藏代码
  1. package com.zte.util;  
  2.   
  3. import java.io.StringWriter;  
  4.   
  5. import org.codehaus.jackson.JsonGenerator;  
  6. import org.codehaus.jackson.map.ObjectMapper;  
  7. /** 
  8.  * JSON工具  
  9.  * 
  10.  */  
  11. public class JSON {  
  12.     private static ObjectMapper mapper = new ObjectMapper();  
  13.       
  14.     /** 
  15.      * 将实体对象转换成JSON格式的字符串 
  16.      * @param obj 
  17.      * @return 
  18.      */  
  19.     public static String toJson(Object obj){  
  20.         String json = "";  
  21.         try {  
  22.             StringWriter writer = new StringWriter();  
  23.             JsonGenerator generator = mapper.getJsonFactory().createJsonGenerator(writer);  
  24.             mapper.writeValue(generator, obj);  
  25.             json = writer.toString();  
  26.             generator.close();  
  27.             writer.close();           
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.           
  32.         return json;  
  33.     }  
  34.       
  35.     /** 
  36.      * JSON格式的字符串转成实体对象 
  37.      * @param json 
  38.      * @param valueType 
  39.      * @return 
  40.      */  
  41.     public static <T> T fromJson(String json, Class<T> valueType){  
  42.         try {  
  43.             return null == json ? null : mapper.readValue(json, valueType);  
  44.         } catch (Exception e) {  
  45.             e.printStackTrace();  
  46.             return null;  
  47.         }  
  48.     }  
  49. }  

 

Java代码  收藏代码
  1. Map<String, Object> result = new HashMap<String, Object>(2);  
  2. try {  
  3.               
  4.         } catch (UploadFileException e) {  
  5.             result.put("code"1);  
  6.             result.put("msg", e.getMessage());  
  7.             writeResponse(response,result);  
  8.             return;  
  9.         }  
  10.   
  11. private void writeResponse(HttpServletResponse response, Map<String, Object> result) {  
  12.         try {  
  13.             response.setContentType("text/html;charset=utf-8");  
  14.             response.getWriter().write(JSON.toJson(result));  
  15.             response.getWriter().flush();  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  

 

 另一个JSON工具类:

Java代码  收藏代码
  1. //Action:  
  2.     String s = this.versionService.saveOrUpdateVersion(version);  
  3.             if (MDSConstants.SUCCESS.equals(s)) {  
  4.                 writeJson(new ResultJson(true, getText("保存成功")));  
  5.             } else {  
  6.                 writeJson(new ResultJson(true, getText("保存失败")));  
  7.             }             
  8.   
  9.     success : function(d) {  
  10.             var json = $.parseJSON(d);  
  11.             if (json.success) {               
  12.                 $.messager.show({title : '系统提示',msg :json.msg});                  
  13.             }else{  
  14.                 $.messager.alert('系统提示',json.msg);  
  15.             }  
  16.         }             
  17.           
  18. 或:  
  19.     List<MngMmField> fields = this.tableService.getFieldsByTableId(oid,userId);  
  20.     if (fields != null && !fields.isEmpty()) {  
  21.         writeJosnArray(fields);  
  22.     } else {  
  23.         writeJosnArray(new ArrayList<MngMmField>());  
  24.     }  
  25.   
  26.     <script type="text/javascript">  
  27.         // JSON 字符串  
  28.         var strJSON = '{"Name":"Tom", "Age":14,"Enable":true}';  
  29.         //  
  30.         var obj = jQuery.parseJSON(strJSON);  
  31.         alert( obj.Name );  
  32.     </script>  

 

Java代码  收藏代码
  1. //结果对象ResultJson.java:            
  2.     public class ResultJson  implements Serializable  
  3.     {  
  4.           private boolean success = false;  
  5.           private String msg = null;  
  6.   
  7.           private Integer resultcode = null;  
  8.           private Object result = null;  
  9.   
  10.           public ResultJson()  
  11.           {  
  12.           }  
  13.   
  14.           public ResultJson(boolean success)  
  15.           {  
  16.             setSuccess(success);  
  17.           }  
  18.   
  19.           public ResultJson(boolean success, String msg, Object result) {  
  20.             setSuccess(success);  
  21.             this.msg = msg;  
  22.             this.result = result;  
  23.           }  
  24.   
  25.           public ResultJson(boolean success, String msg) {  
  26.             setSuccess(success);  
  27.             this.msg = msg;  
  28.           }  
  29.   
  30.           public ResultJson(boolean success, Object result) {  
  31.             setSuccess(success);  
  32.             this.result = result;  
  33.           }  
  34.   
  35.           public String getMsg()  
  36.           {  
  37.             return this.msg;  
  38.           }  
  39.   
  40.           public void setMsg(String msg)  
  41.           {  
  42.             this.msg = msg;  
  43.           }  
  44.   
  45.           public void setResult(Object result)  
  46.           {  
  47.             this.result = result;  
  48.           }  
  49.   
  50.           public Object getResult()  
  51.           {  
  52.             return this.result;  
  53.           }  
  54.   
  55.           public void setSuccess(boolean success)  
  56.           {  
  57.             this.success = success;  
  58.           }  
  59.   
  60.           public boolean isSuccess()  
  61.           {  
  62.             return this.success;  
  63.           }  
  64.   
  65.           public Integer getResultcode()  
  66.           {  
  67.             return this.resultcode;  
  68.           }  
  69.   
  70.           public void setResultcode(Integer resultcode)  
  71.           {  
  72.             this.resultcode = resultcode;  
  73.           }  
  74.     }  

 

Java代码  收藏代码
  1. //JSON.util  
  2.       
  3.     //输出方法  
  4.     public void write(String str) {  
  5.         try {  
  6.             getResponse().setContentType("text/html;charset=utf-8");  
  7.             PrintWriter writer = getResponse().getWriter();  
  8.             writer.write(str);  
  9.             writer.flush();  
  10.             writer.close();  
  11.         } catch (Exception e) {  
  12.               
  13.         }  
  14.     }  
  15.       
  16.     public void writeJson(Object object, String dataFormatter)  
  17.       {  
  18.         if (dataFormatter == null) {  
  19.           dataFormatter = "yyyy-MM-dd";  
  20.         }  
  21.         String json = JSON.toJSONStringWithDateFormat(object, dataFormatter, new SerializerFeature[0]);  
  22.         write(json);  
  23.       }  
  24.   
  25.     public String getJson(Object object, String dataFormatter)  
  26.       {  
  27.         if (dataFormatter == null) {  
  28.           dataFormatter = "yyyy-MM-dd";  
  29.         }  
  30.         return JSON.toJSONStringWithDateFormat(object, dataFormatter, new SerializerFeature[0]);  
  31.       }  
  32.   
  33.     public void writeJson(Object object)  
  34.     {  
  35.         writeJson(object, null);  
  36.     }  
  37.   
  38.     public void writeJsonObject(Object object)  
  39.     {  
  40.         JSONObject jsonObject = JSONObject.fromObject(object);  
  41.         write(jsonObject.toString());  
  42.     }  
  43.   
  44.     public void writeJosnArray(Collection collection)  
  45.     {  
  46.         JSONArray jsonArray = JSONArray.fromObject(collection);  
  47.         System.out.println("xxxd+" + jsonArray.toString());  
  48.         write(jsonArray.toString());  
  49.     }  
0 0
原创粉丝点击