如何在gson中使用缩进生成JSON文件

4
我需要生成一个包含缩进的json文件。我之前使用Jackson来实现,但它会在冒号前添加一个空格,而我不需要这个空格,因此我决定改用Gson
在修改代码后,我发现默认情况下,Gson不使用缩进,但Jackson会使用。有没有人知道在Gson中是否可以获取缩进以及如何实现?
为了使用Gson生成json文件,我进行了以下操作:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.create();
Writer writer = new FileWriter(propsFile); 
gson.toJson(properties, writer);

如果有任何解决方案可以更改Gson中的默认意图,我可以使用Jackson,但是如何消除冒号前的空格? - Eloise
“使用Intent”是什么意思? - David Wasser
1
我相信他/她的意思是“缩进”。 - mihail
已编辑问题并重新标记为适当的标签。 - David Wasser
我确实是指“缩进”。你有什么建议可以解决这个问题吗? - Eloise
你应该发布一个小例子,展示你想要的JSON样式,因为打开pretty print后,JSON已经具有可读性,但是你好像不喜欢这种格式。 - tima
2个回答

2

2

可能是重复的问题,参考链接:https://dev59.com/N53ha4cB1Zd3GeqPcPe6#41509714


使用Jackson并配置它在冒号前不添加空格:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>

CustomPrettyPrinter.java:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;

import java.io.IOException;

class CustomPrettyPrinter extends DefaultPrettyPrinter {
    public CustomPrettyPrinter() {
        super();
    }

    public CustomPrettyPrinter(DefaultPrettyPrinter base) {
        super(base);
    }

    @Override
    public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
        g.writeRaw(": ");
    }

    @Override
    public DefaultPrettyPrinter createInstance() {
        return new CustomPrettyPrinter(this);
    }
}

UseJackson.java:

import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class UseJackson {
    public static void main(String[] args) throws IOException {
        DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("    ", DefaultIndenter.SYS_LF);
        DefaultPrettyPrinter printer = new CustomPrettyPrinter();
        printer.indentArraysWith(indenter);
        printer.indentObjectsWith(indenter);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDefaultPrettyPrinter(printer);

        Properties properties = new Properties();
        properties.put("foo", "3");
        properties.put("bar", "4");
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File("a.json"), properties);
    }
}

使用Gson并配置缩进:

UseGson.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class UseGson {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.put("foo", "3");
        properties.put("bar", "4");

        JsonWriter jsonWriter = new JsonWriter(new FileWriter("a.json"));
        jsonWriter.setIndent("    ");

        Gson gson = new GsonBuilder().serializeNulls().create();
        gson.toJson(properties, Properties.class, jsonWriter);

        jsonWriter.close();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接