java 将 JSON 文件写入为 UTF-8 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29081921/
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-11-02 14:39:46  来源:igfitidea点击:

Write JSON file as UTF-8 encoded

javajsonutf-8

提问by Jon

I am writing a method that writes some JSON to a file, which works fine. However, although I have set the output to be UTF-8, Oxygenfails to read a pound and euro sign.

我正在编写一种将一些 JSON 写入文件的方法,该方法工作正常。但是,尽管我已将输出设置为 UTF-8,但Oxygen无法读取英镑和欧元符号。

Java code:

爪哇代码:

Path logFile = Paths.get(this.output_folder + "/" + file.getName().split("\.")[0] + ".json");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
    File fileDir = new File("test.json");
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));
    ObjectMapper mapper = new ObjectMapper();
    writer.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(all_questions));
}

"all_questions" is an arraylist of Question objects, which is being printed as a formatted piece of JSON by an ObjectMapper.

“all_questions”是一个 Question 对象的数组列表,它被 ObjectMapper 打印为格式化的 JSON 片段。

Some sample JSON with the pound sign looks like this:

一些带有英镑符号的示例 JSON 如下所示:

{
      "name" : "RegExRule",
      "field" : "Q039_4",
      "rules" : [ ],
      "fileName" : "s1rules_england_en.xml",
      "error" : null,
      "pattern_match" : {
        "$record.ApplicationData.SiteVisit.VisitContactDetails.ContactOther.PersonName.PersonGivenName" : "^[\u0000-\u005F\u0061-\u007B\u007d-\u007f£]*$"
      }
}

However, that is displayed in notepad++. In Oxygen, it is displayed as follows:

但是,它显示在记事本++中。在Oxygen中,显示如下:

"pattern_match" : {
        "$record.ApplicationData.SiteVisit.VisitContactDetails.ContactOther.PersonName.PersonGivenName" : "^[\u0000-\u005F\u0061-\u007B\u007d-\u007f?£a??]*$"
 }

回答by Remy Lebeau

When constructing the OutputStreamWriterobject, you need to use "UTF-8"as the charset name, not "UTF8":

构造OutputStreamWriter对象时,您需要使用"UTF-8"作为字符集名称,而不是"UTF8"

new OutputStreamWriter(..., "UTF-8")

Alternatively, use StandardCharsets.UTF_8instead:

或者,StandardCharsets.UTF_8改用:

new OutputStreamWriter(..., StandardCharsets.UTF_8)

Java does not generally support reading/writing BOMs, so if you want your JSON file to have a UTF-8 BOM then you will have to write one manually:

Java 通常不支持读/写 BOM,因此如果您希望 JSON 文件具有 UTF-8 BOM,那么您必须手动编写一个:

Writer out = ...;
out.write("\uFEFF");
out.write(... json content here ...); 

FYI, PrintWritercan manage the OutputStreamWriterand FileOutputStreamobjects for you:

仅供参考,PrintWriter可以为您管理OutputStreamWriterFileOutputStream对象:

Writer out = new PrintWriter(fileDir, "UTF-8");

Or:

或者:

Writer out = new PrintWriter("test.json", "UTF-8");

Lastly, why are you creating a BufferedWriterusing Files.newBufferedWriter()only to ignore it and create a secondary BufferedWritermanually? Why not just use the BufferedWriterthat you already have:

最后,你为什么要创建一个BufferedWriterusing Files.newBufferedWriter()only 来忽略它并BufferedWriter手动创建一个辅助?为什么不直接使用BufferedWriter你已经拥有的:

Path logFile = Paths.get(this.output_folder + "/" + file.getName().split("\.")[0] + ".json");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
    writer.write("\uFEFF");
    ObjectMapper mapper = new ObjectMapper();
    writer.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(all_questions));
}