java 在写入文件之前格式化 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11008313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:24:00  来源:igfitidea点击:

Formatting JSON before writing to File

javajsonHymanson

提问by Brandon

Currently I'm using the Hymanson JSON Processorto write preference data and whatnot to files mainly because I want advanced users to be able to modify/backup this data. Hymanson is awesome for this because its incredibly easy to use and, apparently performs decently (see here), however the only problem I seem to be having with it is when I run myObjectMapper.writeValue(myFile, myJsonObjectNode)it writes all of the data in the ObjectNodeto one line. What I would like to do is to format the JSON into a more user friendly format.

目前我正在使用Hymanson JSON Processor将首选项数据和诸如此类的内容写入文件,主要是因为我希望高级用户能够修改/备份这些数据。Hymanson 在这方面非常棒,因为它非常易于使用,而且显然性能不错(请参阅此处),但是我似乎遇到的唯一问题是当我运行myObjectMapper.writeValue(myFile, myJsonObjectNode)它时,它会将所有数据写入ObjectNode一行。我想做的是将 JSON 格式化为更用户友好的格式。

For example, if I pass a simple json tree to it, it will write the following:

例如,如果我将一个简单的 json 树传递给它,它将写入以下内容:

{"testArray":[1,2,3,{"testObject":true}], "anotherObject":{"A":"b","C":"d"}, "string1":"i'm a string", "int1": 5092348315}

I would want it to show up in the file as:

我希望它在文件中显示为:

{
    "testArray": [
        1,
        2,
        3,
        {
            "testObject": true
        }
    ],
    "anotherObject": {
        "A": "b",
        "C": "d"
    },
    "string1": "i'm a string",
    "int1": 5092348315
}

Is anyone aware of a way I could do this with Hymanson, or do I have to get the String of JSON from Hymanson and use another third party lib to format it?

有没有人知道我可以用 Hymanson 做到这一点的方法,还是我必须从 Hymanson 获取 JSON 字符串并使用另一个第三方库来格式化它?

Thanks in advance!

提前致谢!

采纳答案by Subin Sebastian

try creating Object Writer like this

尝试像这样创建对象编写器

 ObjectWriter writer = mapper.defaultPrettyPrintingWriter();

回答by Marco Lackovic

You need to configure the mapper beforehand as follows:

您需要预先配置映射器,如下所示:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
mapper.writeValue(myFile, myJsonObjectNode);

回答by Sohan

As per above mentioned comments this worked for me very well,

根据上面提到的评论,这对我很有效,

     Object json = mapper.readValue(content, Object.class);
     mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); 

Where contentis your JSON string response

其中content是您的 JSON 字符串响应

Hymanson version:2.12

Hyman逊版本:2.12

回答by vtsamis

To enable standard indentation in Hymanson 2.0.2 and above use the following:

要在 Hymanson 2.0.2 及更高版本中启用标准缩进,请使用以下命令:

ObjectMapper myObjectMapper = new ObjectMapper();
myObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
myObjectMapper.writeValue(myFile, myJsonObjectNode)

source:https://github.com/FasterXML/Hymanson-databind

来源:https: //github.com/FasterXML/Hymanson-databind