国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java中JSON處理工具類使用詳解

2024-07-13 10:17:25
字體:
來源:轉載
供稿:網友

本文實例為大家分享了JSON處理工具類的具體代碼,供大家參考,具體內容如下

import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map;  import javax.servlet.http.HttpServletResponse;  import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature;  /**  *  * @author humf  *  */ public class FastJsonUtil {      /**    * 將對象轉成json串    * @param object    * @return    */   public static String toJSONString(Object object){     //DisableCircularReferenceDetect來禁止循環引用檢測     return JSON.toJSONString(object,SerializerFeature.DisableCircularReferenceDetect);   }      //輸出json   public static void write_json(HttpServletResponse response,String jsonString){     response.setContentType("application/json;utf-8");     response.setCharacterEncoding("UTF-8");     try {       response.getWriter().print(jsonString);     } catch (IOException e) {       e.printStackTrace();     }     }      /**    * ajax提交后回調的json字符串    * @return    */   public static String ajaxResult(boolean success,String message)   {     Map map=new HashMap();     map.put("success", success);//是否成功     map.put("message", message);//文本消息     String json= JSON.toJSONString(map);         return json;   }       /**    * JSON串自動加前綴    * @param json 原json字符串    * @param prefix 前綴    * @return 加前綴后的字符串    */    public static String JsonFormatterAddPrefix(String json,String prefix,Map<String,Object> newmap)   {     if(newmap == null){       newmap = new HashMap();     }     Map<String,Object> map = (Map) JSON.parse(json);      for(String key:map.keySet())     {       Object object=map.get(key);       if(isEntity(object)){         String jsonString = JSON.toJSONString(object);         JsonFormatterAddPrefix(jsonString,prefix+key+".",newmap);                }else{         newmap.put(prefix+key, object);       }            }     return JSON.toJSONString(newmap);       }   /**    * 判斷某對象是不是實體    * @param object    * @return    */   private static boolean isEntity(Object object)   {     if(object instanceof String )     {       return false;     }     if(object instanceof Integer )     {       return false;     }     if(object instanceof Long )     {       return false;     }     if(object instanceof java.math.BigDecimal )     {       return false;     }     if(object instanceof Date )     {       return false;     }     if(object instanceof java.util.Collection )     {       return false;     }     return true;        } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊宁县| 门头沟区| 丹江口市| 奈曼旗| 观塘区| 乌恰县| 勃利县| 三河市| 赣榆县| 尉氏县| 凤凰县| 北碚区| 东源县| 兴业县| 阳谷县| 敖汉旗| 永城市| 满洲里市| 双柏县| 鹤岗市| 武冈市| 茶陵县| 保亭| 襄城县| 临漳县| 荥经县| 孟村| 贡嘎县| 德庆县| 贵定县| 靖远县| 黑龙江省| 祁阳县| 英山县| 安丘市| 西林县| 雅安市| 洪雅县| 嘉禾县| 洪雅县| 金寨县|