【问题标题】:How can i add a linebreak with javascript at a json file如何在 json 文件中使用 javascript 添加换行符
【发布时间】:2018-11-26 02:19:32
【问题描述】:

我现在如何在此处添加换行符???我想更好地查看报告,而不仅仅是一行,因为它不可读,所以有人可以帮助我吗?

const Discord = require("discord.js");
const fs = require("fs");
const ms = require("ms");
let bugreport = JSON.parse(fs.readFileSync("./bugreports.json", "utf8"));
let bugreportlineid = JSON.parse(fs.readFileSync("./bugreports.json", "utf8"));

module.exports.run = async (bot, message, args) => {
let bugReason = args.join(" ");
let bugID = Math.floor(Math.random() * 1337 / 3 * 1337)
let bugReporter = message.author.tag;

if(!bugreport[bugID]) bugreport[bugID] = {
    BugID: bugID,
    User: bugReporter,
    Bugreport: bugReason
  };

fs.writeFile("./bugreports.json",  JSON.stringify(bugreport), (err) => {
    if(err) console.log(err)
});

message.channel.send(`Bugreport with the ID: ${bugID} sucessfully sended!`)
}

module.exports.help = {
name: "bugreport"
}

【问题讨论】:

  • stackoverflow dont detects the code. 错误。请阅读How to Ask...
  • 请不要指望我们出去调试您的现场网站。在您的问题中发布相关代码。
  • 并且通过将json以换行符分隔的方式写入文件来使json无效是没有意义的。改为使用对象数组。
  • 它仍然没有检测到。我做了stackflow告诉我的一切......
  • 单击问题中的“编辑”链接并添加您正在使用的代码。然后,突出显示该代码并在工具栏上单击{} 按钮将文本格式化为代码。 Stack Overflow 不会阻​​止您这样做。

标签: javascript json discord.js


【解决方案1】:

如果您想要漂亮地打印 JSON 输出,请使用 space 参数到 JSON.stringify

fs.writeFile("./bugreports.json",  JSON.stringify(bugreport, null, 2), (err) => {
    if(err) console.log(err)
});

【讨论】:

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