Export json string to json file in Java [detailed steps]

I. Overview

Please make specific changes according to specific needs. This code needs to query the front-end data, and then convert the json string

The .getCatalogId(id) method is to find out the content according to the id and then convert it into a json string

You can also directly pass in the json string for testing

Two, the code 

    @ApiOperation("导出为json文件")
    @PostMapping("/export")
    @DisableEncryptResponse
    public R<String> export(@RequestBody Long id) {
        R<Object> list = scenarioService.getCatalogId(id);
        String jsonString = JSON.toJSONString(list);

        String filePath = "D:\\temp";
        String fileName = "test";
        String fullPath = filePath + File.separator + fileName + ".json";
        //例如:fullPath="D:/temp/test.json"
        String address = fileName + ".json";

        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(fullPath);
            if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();

            // 格式化json字符串
            jsonString = formatJson(jsonString);

            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(jsonString);
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok(address);
    }
package com.dbm.flowconfig.util;

public class json {
    /**
     * 单位缩进字符串。
     */
    private static String SPACE = "   ";

    /**
     * 返回格式化JSON字符串。
     *
     * @param json 未格式化的JSON字符串。
     * @return 格式化的JSON字符串。
     */
    public static String formatJson(String json) {
        StringBuffer result = new StringBuffer();

        int length = json.length();
        int number = 0;
        char key = 0;

        // 遍历输入字符串。
        for (int i = 0; i < length; i++) {
            // 1、获取当前字符。
            key = json.charAt(i);

            // 2、如果当前字符是前方括号、前花括号做如下处理:
            if ((key == '[') || (key == '{')) {
                // (1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
                if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) {
                    result.append('\n');
                    result.append(indent(number));
                }

                // (2)打印:当前字符。
                result.append(key);

                // (3)前方括号、前花括号,的后面必须换行。打印:换行。
                result.append('\n');

                // (4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。
                number++;
                result.append(indent(number));

                // (5)进行下一次循环。
                continue;
            }

            // 3、如果当前字符是后方括号、后花括号做如下处理:
            if ((key == ']') || (key == '}')) {
                // (1)后方括号、后花括号,的前面必须换行。打印:换行。
                result.append('\n');

                // (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
                number--;
                result.append(indent(number));

                // (3)打印:当前字符。
                result.append(key);

                // (4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。
                if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
                    result.append('\n');
                }

                // (5)继续下一次循环。
                continue;
            }

            // 4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
            if ((key == ',')) {
                result.append(key);
                result.append('\n');
                result.append(indent(number));
                continue;
            }

            // 5、打印:当前字符。
            result.append(key);
        }

        return result.toString();
    }

    /**
     * 返回指定次数的缩进字符串。每一次缩进三个空格,即SPACE。
     *
     * @param number 缩进次数。
     * @return 指定缩进次数的字符串。
     */
    private static String indent(int number) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < number; i++) {
            result.append(SPACE);
        }
        return result.toString();
    }
}

3. Use postman for testing

The exported file is under the temp folder on the D drive

The following is a sample of json format

{

    "teacher": [

        {

            "id": "001",

            "name": "Zhang San"

        },

        {

            "id": "002",

            "name": "Li Si"

        }

    ],

    "student": [

        {

            "id": "101",

            "name": "Wang Wu"

        }

    ]

}

 Fourth, use nginx to download json files

If the front-end wants to get your json file, you can configure nginx and send it to the front-end download URL, and download it in the URL

 Right-click nginx.conf, use notepad++ to open, configure the download address, after configuration, the download address is  http://your IP address/download/test.json

 

 

 so that others can download it through the link you provided

Five, JSON string, JSON object, JSON array, entity class conversion

http://t.csdn.cn/FS7ev 

Guess you like

Origin blog.csdn.net/Jiang5106/article/details/130215689