java代码如何实现JSON格式化和压缩成一行呢?

乔欣 Java经验 发布时间:2023-02-21 20:45:25 阅读数:1130 1
下文笔者讲述使用java代码实现JSON格式化和输出压缩的方法分享

JSON简介

 
 JSON(JavaScript Object Notation, JS对象):
     是一种轻量级的数据交换格式
     采用完全独立于编程语言的文本格式来存储和表示数据
     简洁和清晰的层次结构使得JSON成为理想的数据交换语言
JSON是目前市面上最流行的交互语言

JSON示例

 
{
	"address":"猫猫大街",
	"name":"JSON String",
	"age":"29",
	"addressTest":"无对应属性,不转换"
}
//JSON压缩为一行
{"name":"JSON String","age":"29","address":"猫猫大街","addressTest":"无对应属性,不转换"}

JSON工具类
将JSON转换为一行

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里面的空格、回车、换行符、制表符等
 */
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\":\"猫猫大街\",\"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));
    }   
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16769839985872.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者