JSON格式化和壓縮成一行
摘要:把JSON字符串壓縮成一行,即去掉中的空格、回車、換行符、制表符等;格式化JSON, 易於閱讀。
JSON(JavaScript Object Notation, JS 對象簡譜) 是一種輕量級的數據交換格式,采用完全獨立於編程語言的文本格式來存儲和表示數據。簡潔和清晰的層次結構使得 JSON 成為理想的數據交換語言。 易於人閱讀和編寫,同時也易於機器解析和生成,並有效地提升網絡傳輸效率。
無論是數據的傳輸還是存儲,在很多應用場景下,可能想壓縮JSON字符串的長度,以提升傳輸效率。你如果使用的是nosql數據庫,可能想進一步的壓縮json字符串的長度來節省存儲空間,要怎么才能快速的去掉這些空格和換行符等呢?本文介紹如何使用代碼去掉字符串里面的空格、回車、換行符、制表符等,但是,刪除空格的時候,只刪除兩個連在一起的空格,不刪除兩個字符中間的單個字符。例如將如下json壓縮成一行:
{
"address":"河南省 商丘師范學院,坐標: x, y",
"name":"JSON String",
"age":"29",
"addressTest":"無對應屬性,不轉換"
}
壓縮后的結果:
{"name":"JSON String","age":"29","address":"河南省 商丘師范學院,坐標: x, y","addressTest":"無對應屬性,不轉換"}
JSON工具類
定義JSON工具類JsonUtil:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 格式化json<br/>
* java去掉String里面的空格、回車、換行符、制表符等
* @Author: Wiener
* @Date: 2021/9/25 19:52
*/
public class JsonUtil {
public static final String EMPTY = "";
/**
* 替換所有空格,留下一個
*/
private static final String REPLACE_BLANK_ENTER = "\\s{2,}|\t|\r|\n";
private static final Pattern REPLACE_P = Pattern.compile(REPLACE_BLANK_ENTER);
/**
* 使用正則表達式刪除字符串中的空格、回車、換行符、制表符
* @param str
* @return
*/
public static String replaceAllBlank(String str) {
String dest = "";
if (StringUtils.isNotBlank(str)) {
Matcher m = REPLACE_P.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 去除字符串中的空格、回車、換行符、制表符
* \n 回車(\u000a)
* \t 水平制表符(\u0009)
* \s 空格(\u0008)
* \r 換行(\u000d)
* @param source
* @return
*/
public static String replaceBlank(String source) {
String ret = EMPTY;
if (StringUtils.isNotBlank(source)) {
ret = source.replaceAll(StringUtils.LF, EMPTY)
.replaceAll("\\s{2,}", EMPTY)
.replaceAll("\\t", EMPTY)
.replaceAll(StringUtils.CR, EMPTY);
}
return ret;
}
/**
* 使用fastjson JSONObject格式化輸出JSON字符串
* @param source
* @return
*/
public static String formatJson(String source) {
JSONObject object = JSONObject.parseObject(source);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
return pretty;
}
public static String formatJsonOneRow(String source) {
JSONObject object = JSONObject.parseObject(source);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
return pretty;
}
public static void main(String[] args) {
String source = "{\"name\":\"JSON String\",\"age\":\"29\",\"address\":\"河南省 商丘師范學院,坐標: x, y\",\"addressTest\":\"無對應屬性,不轉換\"}";
System.out.println("-------formatJson---------");
System.out.println(formatJson(source));
System.out.println("-------replaceBlank---------");
System.out.println(replaceBlank(source));
System.out.println("-------replaceAllBlank---------");
System.out.println(replaceAllBlank(source));
}
}
測試結果如下:
-------formatJson---------
{
"address":"河南省 商丘師范學院,坐標: x, y",
"name":"JSON String",
"age":"29",
"addressTest":"無對應屬性,不轉換"
}
-------replaceBlank---------
{"name":"JSON String","age":"29","address":"河南省 商丘師范學院,坐標: x, y","addressTest":"無對應屬性,不轉換"}
-------replaceAllBlank---------
{"name":"JSON String","age":"29","address":"河南省 商丘師范學院,坐標: x, y","addressTest":"無對應屬性,不轉換"}
小結
工作中處處都需要學習,有時候看似簡單的一個優化,可以讓你深入學習后收獲各種知識。所以在學習中要不求甚解,不僅要了解這個知識點,也要熟悉為什么要這么做。知行合一:學習完一個知識點要盡量及時實踐,這樣才能記得牢靠。