【发布时间】:2021-11-12 15:42:35
【问题描述】:
我试图从这个出发:
{
"Level":"A",
"Group LVL2":{
"Level":"B",
"Group LVL3":{
"Level":"C"
}}}
到这里
{
"Level":"C",
"Group LVL2":{
"Level":"C",
"Group LVL3":{
"Level":"C"
}}}
所以我基本上想将 json 键的所有值替换为相同的值。 这是我正在使用的代码的部分:
const fs = require('fs');
const fileName = './' + (Profile) + ".json";
const file = require(fileName);
const key = (Root);
file[Root] = (Value);
fs.writeFile(fileName, JSON.stringify(file, null, 2), function writeJSON(error) {
if (error) return console.log(error);
但它只替换了第一个json组/行的Level Value:
{
"Level":"THIS WILL BE REPLACED",
"Group LVL2":{
"Level":"THIS WILL NOT BE REPLACED",
"Group LVL3":{
"Level":"THIS WILL NOT BE REPLACED"
}
}
}
希望我能找到解决方案,我指望你! (网上似乎没有任何适合初学者的解决方案)
【问题讨论】: