如何在 JSON 字段之间添加换行和适当的缩进?

huangapple go评论89阅读模式
英文:

How to add line drop and appropriate tabs between Json fields?

问题

我写了这段代码:

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 字段之间添加换行和适当的制表符?

英文:

I wrote this code:

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

I got result like:

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

I want the content will be:

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

I want it to be with drop line in any bracket, and number of tabs compatible.
How can I add line drop and appropriate tabs between Json fields?

答案1

得分: 0

我认为仅通过配置漂亮的打印机是无法实现这一点的,您将不得不为此实现自己的类。请参阅 https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/PrettyPrinter.html

英文:

I don't think you can achieve this simply by configuring the pretty printer, you will have to implement your own class for this. See https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/PrettyPrinter.html

答案2

得分: 0

为了实现类似这样的格式,您需要实现自己的 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"
      }
    ]
  }
}

这将打印出类似于您所期望的格式。

英文:

To achieve format like this you need to implement your own 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);
	}
}

And you can use it as below:

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);
	}
}

above code prints:

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

which prints something similar to what you want.

huangapple
  • 本文由 发表于 2020年10月21日 16:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64459378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定