Java中JSON處理工具類使用詳解
更新時間:2018年01月27日 12:00:40 作者:御前提筆小書童
這篇文章主要為大家詳細(xì)介紹了Java中JSON處理工具類的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JSON處理工具類的具體代碼,供大家參考,具體內(nèi)容如下
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 {
/**
* 將對象轉(zhuǎn)成json串
* @param object
* @return
*/
public static String toJSONString(Object object){
//DisableCircularReferenceDetect來禁止循環(huán)引用檢測
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提交后回調(diào)的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);
}
/**
* 判斷某對象是不是實(shí)體
* @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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗(yàn)實(shí)例
本篇文章主要介紹了詳解springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗(yàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
dependencies導(dǎo)致的Maven依賴出錯包紅問題解決方法
多模塊和分布式開發(fā)一般都是有專門的的dependencies來進(jìn)行jar包的版本依賴問題,本文主要介紹了dependencies導(dǎo)致的Maven依賴出錯包紅問題解決方法,具有一定的參考價值,感興趣的可以了解一下2022-05-05
java基本教程之常用的實(shí)現(xiàn)多線程的兩種方式 java多線程教程
下面開始學(xué)習(xí)“常用的實(shí)現(xiàn)多線程的2種方式”:Thread 和 Runnable。之所以說是常用的,是因?yàn)橥ㄟ^還可以通過java.util.concurrent包中的線程池來實(shí)現(xiàn)多線程2014-01-01
SWT(JFace) FTP客戶端實(shí)現(xiàn)
SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)2009-06-06
淺談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用
下面小編就為大家?guī)硪黄獪\談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
IDEA 2021.1 操作SVN 最新超詳細(xì)教程(圖文)
本教程將通過idea從svn服務(wù)器中的任意一個分支檢出代碼(本文采用branches),然后再idea中創(chuàng)建新的分支、提交代碼、拉取代碼、合并分支等操作進(jìn)行一一記錄,暫不包含代碼合并,對idea2021.1操作svn相關(guān)知識感興趣的朋友一起學(xué)習(xí)下吧2021-05-05

