gpt4 book ai didi

json - 如何将 JSON 数据 append 到现有的 JSON 文件 node.js

转载 作者:行者123 更新时间:2023-12-04 14:23:37 40 4
gpt4 key购买 nike

如何使用逗号“,”作为分隔符 append 现有的 JSON 文件

anchors = [ {  "title":"  2.0 Wireless " }  ]
fs.appendFileSync('testOutput.json', JSON.stringify(anchors));

这个当前代码的输出是这样的
[
{
"title":" 2.0 Wireless "
}
]
[
{
"title":" Marshall Major II "
}
]

如何以正确的格式使用逗号“,”作为分隔符

我想得到这样的东西
[
{
"title":" 2.0 Wireless "
},
{
"title":" Marshall Major II "
}
]

最佳答案

尝试这个。不要忘记定义 anchors大批。

var data = fs.readFileSync('testOutput.json');
var json = JSON.parse(data);
json.push(...anchors);

fs.writeFile("testOutput.json", JSON.stringify(json))

关于json - 如何将 JSON 数据 append 到现有的 JSON 文件 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747537/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com