如何在Json字段之间添加换行和适当的选项卡?

问题描述

我写了这段代码

ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try
{
    writer.writeValue(new File(jsonFile.getAbsolutePath()),jsonData);
}
catch (IOException e)
{
}

我得到的结果是:

{
  "type" : "aaa","key" : {
    "key1" : "bbb","key 2" : [ {
      "value" : "xx"
    },{
      "value" : "sss"
    },{
      "value" : "zzz"
    }]
  }
}

我希望内容为:

{
  "type" : "aaa","key" : 
   {
    "key1" : "bbb","key 2" :
    [
      {
        "value" : "xx"
      },{
        "value" : "sss"
      },{
        "value" : "zzz"
      }
    ]
  }
}

我希望它在任何括号中都带有下划线,并且兼容的制表符数量。 如何在Json字段之间添加换行符和适当的选项卡?

解决方法

我认为您不能仅通过配置漂亮的打印机来实现此目的,您将必须为此实现自己的类。参见https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/PrettyPrinter.html

,

要实现这种格式,您需要实现自己的PrettyPrinter

class ObjectArrayInNewLinePrettyPrinter extends DefaultPrettyPrinter {

    public ObjectArrayInNewLinePrettyPrinter() {
        super();
    }

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

    @Override
    public void writeStartObject(JsonGenerator g) throws IOException {
        _objectIndenter.writeIndentation(g,_nesting);
        super.writeStartObject(g);
    }

    @Override
    public void writeStartArray(JsonGenerator g) throws IOException {
        _arrayIndenter.writeIndentation(g,_nesting);
        super.writeStartArray(g);
    }

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

您可以按以下方式使用它:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.File;
import java.io.IOException;

public class JsonPrettyPrinterApp {
    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        DefaultPrettyPrinter printer = new ObjectArrayInNewLinePrettyPrinter();
        printer.indentArraysWith(new DefaultIndenter());

        JsonMapper mapper = JsonMapper.builder()
                .enable(SerializationFeature.INDENT_OUTPUT)
                .defaultPrettyPrinter(printer)
                .build();

        JsonNode root = mapper.readTree(jsonFile);

        mapper.writeValue(System.out,root);
    }
}

以上代码打印:

{
  "type" : "aaa","key" : 
  {
    "key1" : "bbb","key 2" : 
    [
      
      {
        "value" : "xx"
      },{
        "value" : "sss"
      },{
        "value" : "zzz"
      }
    ]
  }
}

打印出与您想要的东西相似的东西。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...