【问题标题】:How to convert pretty format json file into a simple single line json file using java?如何使用 java 将漂亮格式的 json 文件转换为简单的单行 json 文件?
【发布时间】:2021-05-20 03:18:52
【问题描述】:

我需要将漂亮格式的 json 文件转换为单行 json 文件。但无法这样做。谁能帮我解决这个问题?

示例:以下需要转换

[
 {
  "Employee ID": 1,
  "Name": "Abhishek",
  "Designation": "Software Engineer"
 },
 {
  "Employee ID": 2,
  "Name": "Garima",
  "Designation": "Email Marketing Specialist"
 }
]
[
   {
      "Employee ID": 1,
      "Name": "Abhishek",
      "Designation": "Software Engineer"
   },
   {
      "Employee ID": 2,
      "Name": "Garima",
      "Designation": "Email Marketing Specialist"
   }
]

到这里:

 '[{"Employee ID":1,"Name":"Abhishek","Designation":"Software Engineer"},' \ 
            '{"Employee ID":2,"Name":"Garima","Designation":"Email Marketing Specialist"}]'

【问题讨论】:

  • 如果你真的需要得到结果文件,而不是内存中的整个文本,我不会使用下面的收据,而只是逐行读取原始文件,修剪并将结果字符串写入输出文件: try (BufferedWriter/FileWiter output = ...; Stream stream = Files.lines(Paths.get(fileName))) { stream.forEach( /* 修剪并写入结果字符串到输出}}。只存储你的工作真正需要的东西,节省你的资源:)
  • 有道理,谢谢! :)

标签: java json string


【解决方案1】:

this answer 的更好、更完整的版本,具有适当的错误处理和删除多余的空格。

引用答案的一个问题是它使用.concat() 而不使用.trim() - 嵌套非常深的漂亮JSON 的缩进将在最终输出中显示为额外的空格。

String unprettyJSON = null;

try {
    unprettyJSON = Files.readAllLines(Paths.get("pretty.json"))
                        .stream()
                        .map(String::trim)
                        .reduce(String::concat)
                        .orElseThrow(FileNotFoundException::new);
} catch (IOException e) {
    e.printStackTrace();
}

带修剪的输出:

[{"Employee ID": 1,"Name": "Abhishek","Designation": "Software Engineer"},{"Employee ID": 2,"Name": "Garima","Designation": "Email Marketing Specialist"}][{"Employee ID": 1,"Name": "Abhishek","Designation": "Software Engineer"},{"Employee ID": 2,"Name": "Garima","Designation": "Email Marketing Specialist"}]

没有修剪的输出:

[ {  "Employee ID": 1,  "Name": "Abhishek",  "Designation": "Software Engineer" }, {  "Employee ID": 2,  "Name": "Garima",  "Designation": "Email Marketing Specialist" }][   {      "Employee ID": 1,      "Name": "Abhishek",      "Designation": "Software Engineer"   },   {      "Employee ID": 2,      "Name": "Garima",      "Designation": "Email Marketing Specialist"   }]

【讨论】:

  • Domenico Sibilio 和 Harshal Parekh 按预期工作,谢谢!!
  • 嗨@Harshal Parekh,它按预期工作,但我的 json 非常大,任何使过程变胖的方法,我都需要一些时间
【解决方案2】:

如果您通过Files.readAllLines(Path) 读取文件内容,那么您已经将每一行作为列表中的不同项目,您可以依次合并为一行。

例如:

String oneLine = Files.readAllLines(Paths.get("input.json"))
.stream()
.reduce(String::concat)
.orElseThrow();

System.out.println(oneLine);

【讨论】:

    【解决方案3】:
    try {
        unprettyJSON = Files.readAllLines(Paths.get("pretty.json"))
                            .stream()
                            .map(String::trim)
                            .reduce(String::concat)
                            .orElseThrow(FileNotFoundException::new);
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    上述解决方案效果很好。感谢您发布它。您可以从 .json 文件中读取,该文件转换为单行字符串,也可用于 POST https 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多