用Java将格式化后的json字符串压缩为一行

  1. 实现代码:
package com.yang.restdemo.state;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: Yang JianXiong
 * @since: 2022/12/5
 */
public class Context<T> {

    /*BaseState state;

    public Context(BaseState state) {
        this.state = state;
    }

    public Object doAction() {
        Objects.requireNonNull(this.state);
        return this.state.doAction();
    }*/

    public static void main(String[] args) throws IOException {
        // 0.原始多行json
        String originJson = "{\n" +
                "    \"forceSelfExpense\": false,\n" +
                "    \"patientId\": \"115801\",\n" +
                "    \"cardInfo\": {\n" +
                "        \"cardId\": \"1\",\n" +
                "        \"residentCardNo\": \"11010119900307061X\"\n" +
                "    },\n" +
                "    \"hospitalizedApplyRequest\": {\n" +
                "        \"applyAreaId\": \"40033004\",\n" +
                "        \"applyAreaName\": \"三病区\",\n" +
                "        \"applyCampusId\": \"30012005\",\n" +
                "        \"applyDeptId\": \"40033002\",\n" +
                "        \"applyDeptName\": \"泌尿外科(住院)\",\n" +
                "        \"attendingDoctorId\": \"40102\",\n" +
                "        \"attendingDoctorName\": \"主任医师\",\n" +
                "        \"diagnosisDictIdList\": [\n" +
                "            \"4922111\"\n" +
                "        ],\n" +
                "        \"applyHospitalizedTime\": \"2022-11-29 00:00:00\",\n" +
                "        \"patientId\": \"115801\",\n" +
                "        \"patientName\": \"鹏鹏\"\n" +
                "    },\n" +
                "    \"discountCode\": [],\n" +
                "    \"importanceFlagCodes\": []\n" +
                "}";

        // 1.字符串转为字节数组
        byte[] byteArray = originJson.getBytes(StandardCharsets.UTF_8);

        // 2.构造字节数组输入流
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);

        // 3.构造输入流读取器
        InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream); //, StandardCharsets.UTF_8);

        // 4.构造缓冲型读取器
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String line;
        StringBuilder stringBuilder = new StringBuilder();
        // 5.循环读取每行字符串,并做必要处理
        while ((line = bufferedReader.readLine()) != null) {
            // 去掉每行两端的空格,并重新拼接为一行
            stringBuilder.append(line.trim());
        }

        // 6.结果输出
        System.err.println(stringBuilder);
    }

}
  1. 输出(每行内,比如冒号后的空格未去掉,可以在循环体内做增强):
{"forceSelfExpense": false,"patientId": "115801","cardInfo": {"cardId": "1","residentCardNo": "11010119900307061X"},"hospitalizedApplyRequest": {"applyAreaId": "40033004","applyAreaName": "三病区","applyCampusId": "30012005","applyDeptId": "40033002","applyDeptName": "泌尿外科(住院)","attendingDoctorId": "40102","attendingDoctorName": "主任医师","diagnosisDictIdList": ["4922111"],"applyHospitalizedTime": "2022-11-29 00:00:00","patientId": "115801","patientName": "鹏鹏"},"discountCode": [],"importanceFlagCodes": []}
posted @ 2022-12-06 14:26  JaxYoun  阅读(1782)  评论(0编辑  收藏  举报