Java中JSON处理工具类使用详解
作者:御前提笔小书童 时间:2023-09-19 17:59:08
本文实例为大家分享了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;
}
}
来源:http://blog.csdn.net/qq_22260641/article/details/72910273
标签:Java,JSON,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java8中stream和functional interface的配合使用详解
2023-02-27 22:44:51
Java注解(annotation)简述
2022-03-07 08:10:12
![](https://img.aspxhome.com/file/2023/4/62824_0s.png)
Java中的注解和反射实例详解
2023-02-02 04:25:45
![](https://img.aspxhome.com/file/2023/0/65780_0s.png)
Android中ListView的几种常见的优化方法总结
2021-06-09 08:36:21
![](https://img.aspxhome.com/file/2023/9/116009_0s.png)
Android实现五子棋游戏(局域网版)
2023-04-08 08:17:20
![](https://img.aspxhome.com/file/2023/0/126680_0s.jpg)
史上最全的java随机数生成算法分享
2023-10-17 15:22:33
![](https://img.aspxhome.com/file/2023/9/62759_0s.jpg)
Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法
2023-07-21 10:51:45
深入理解Java设计模式之状态模式
2022-07-07 12:49:44
![](https://img.aspxhome.com/file/2023/1/109941_0s.jpg)
Spring Boot 中application.yml与bootstrap.yml的区别
2023-07-09 12:30:56
C#对象为Null模式(Null Object Pattern)实例教程
2023-07-27 15:53:28
![](https://img.aspxhome.com/file/2023/8/89788_0s.png)
基于String和List<String>间的相互转换方式
2022-09-25 15:52:21
![](https://img.aspxhome.com/file/2023/0/116180_0s.png)
Java 转型(向上或向下转型)详解及简单实例
2021-10-17 14:29:27
Java集合TreeSet用法详解
2023-11-10 22:53:34
![](https://img.aspxhome.com/file/2023/4/59354_0s.jpg)
Android可配置透明度的水印
2021-06-06 14:07:42
![](https://img.aspxhome.com/file/2023/1/89551_0s.png)
Android判断某个权限是否开启的方法
2021-06-28 07:42:15
![](https://img.aspxhome.com/file/2023/3/128953_0s.jpg)
SpringBoot 如何整合 ES 实现 CRUD 操作
2022-03-24 03:10:31
![](https://img.aspxhome.com/file/2023/6/108446_0s.png)
Unity3D应用之时钟与钟表小组件的使用教程
2023-05-15 04:43:35
![](https://img.aspxhome.com/file/2023/1/91361_0s.png)
解析JAVA深度克隆与浅度克隆的区别详解
2023-11-02 10:57:28
Java中的private、protected、public和default的区别(详解)
2023-01-10 01:55:51
Java实现选择排序
2021-06-30 16:53:55
![](https://img.aspxhome.com/file/2023/2/84602_0s.jpg)