如何编辑.json对象的值
我正在尝试在 .JSON 文件中创建一些简单的统计信息,我想对发出的每个命令进行计数,但我无法在 .JSON 文件中保存增量值。
.JSON
{
"stats": {
"value": 0,
"points": 0,
"commandUsed": 0
}
}
代码:
const fs = require('fs');
let statistics = fs.readFileSync(__dirname + '/stats.json', 'utf8');
let stats = JSON.parse(statistics)
console.log(stats)
//stats
let value = stats['stats']['value']
let points = stats['stats']['points']
let usedCommands = stats['stats']['commandUsed']
usedCommands++
console.log(usedCommands) //logs actual amount of issued commands
fs.writeFileSync(__dirname + '/stats.json', JSON.stringify(stats, 0, 4), 'utf8')
.JSON 文件中的命令计数没有增加。
I'm trying to create some simple statistics in the .JSON file, I would like to count each command that was issued, but I'm unable to save increment value in the .JSON file.
.JSON
{
"stats": {
"value": 0,
"points": 0,
"commandUsed": 0
}
}
code:
const fs = require('fs');
let statistics = fs.readFileSync(__dirname + '/stats.json', 'utf8');
let stats = JSON.parse(statistics)
console.log(stats)
//stats
let value = stats['stats']['value']
let points = stats['stats']['points']
let usedCommands = stats['stats']['commandUsed']
usedCommands++
console.log(usedCommands) //logs actual amount of issued commands
fs.writeFileSync(__dirname + '/stats.json', JSON.stringify(stats, 0, 4), 'utf8')
The command count is not increasing in the .JSON file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到有几件事。您有一些不使用的VAR,并且您试图“增加”的内容是JSON文件中的一个字符串(已更新)。尝试一下。
There are a couple of things I noticed. You had a few vars that you were not using and what you were trying to "increment" was a string in your JSON file (updated). Try this.