Let’s say I need to serialize data as a JSON string and write it to a data.json file:
1 let str = JSON.stringify(data)
2
3 fs.writeFile('data.json',str,function(err){
4 if (err) {res.status(500).send('Server is error...')}
5 })
Copy the code
When you put it in a JSON file, you get a really nasty line, look at that
Can’t stand it right!!
Vanilla JS’s json.stringify () made the output beautiful and intuitive.
The first piece of code can be written like this, with the third argument set to “\t” (the second argument must be filled, which can be written as “” or null) :
let str = JSON.stringify(data,"","\t")
let str = JSON.stringify(data,null,"\t")
Copy the code
Then data.json can do this