繁体   English   中英

如何将文本添加到现有的json数组中?

[英]How to add text into already existing json array?

我有一个nodejs应用程序,我需要一个命令的帮助。 当用户运行命令时,他们为它提供一个id,并将该id添加到json数组中。

我尝试了一切

{
"blist": ["1038494838","83857393984", and so on ]
}
const blacklist = require('./blist.json')

我尝试过的所有事情都没有错误。 它只是没有添加任何文件。

你可以使用.push()

blist.push(userInput) //will add the user's input at the end of the array

并将其写入对象中

var json = JSON.stringify(blist);

而实际写在里面我推荐fs

const fs = require('fs');
fs.writeFile('blist.json', json, 'utf8', callback);

就是这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM