如何在json文件中添加带有javascript的换行符

时间:2018-06-16 15:12:03

标签: javascript json discord.js

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

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"
}

1 个答案:

答案 0 :(得分:1)

如果你想要的是打印JSON输出,请使用fs.writeFile("./bugreports.json", JSON.stringify(bugreport, null, 2), (err) => { if(err) console.log(err) }); 参数JSON.stringify

var data = [];

axios.get('https://jsonplaceholder.typicode.com/posts')
    .then(function (response) {
        data.push(response.data['0'])
    })
    .catch(function (error) {
        console.log(error);
    });
console.log(data);
console.log(data['0'].body); //this is where I get the error