具体内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| 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;
public class FastJsonUtil {
public static String toJSONString(Object object){ return JSON.toJSONString(object,SerializerFeature.DisableCircularReferenceDetect); } 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(); } }
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; }
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); }
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; } }
|
本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。
本文链接:https://royalscholar.cn/2017/06/08/Java中JSON处理工具类/