大佬教程收集整理的这篇文章主要介绍了Java中JSON处理工具类使用详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例为大家分享了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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的Java中JSON处理工具类使用详解全部内容,希望文章能够帮你解决Java中JSON处理工具类使用详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。