简体   繁体   English

如何使用 node.js 替换 json 文件的特定部分

[英]How to replace specific parts of a json file using node.js

Currently, I have a JSON file that looks like this:目前,我有一个如下所示的 JSON 文件:

"1E10BC5D4EC68EE2916BFD97F23E951C": "Seattle Seahawks",
"E6B87019417436B73B62F7802763A289": "Seaside style. ",
"81EEA9E6400BFEADE161559AF14EE468": " {1}",
"6F02148E4A78B33C1CEB75BC2753CA69": " {EndDate}",
"D89CA2634FFF8FA02D028096BAAE6963": "\"You have received a reward for completing the {Bundle Name} {Number} challenges!",
"Mission": {
    "Default Mission Info Description": "Default Mission Description",
    "Default Mission Info Name": "Default Mission Name",
    "RewardDescription": "You ranked {PositionRank}. For your efforts, you have been awarded:",
    "NonParticipationRewardDescription": "Your teammates did a great job! For their efforts, you have been awarded:",
    "RewardTitle": "{MissionName} Completed!"
  }

It goes on for about 40,000 lines, and I would like to modify all of the strings set by it.它持续了大约 40,000 行,我想修改它设置的所有字符串。 Currently, I am using @zuzak/owo to try to accomplish this.目前,我正在使用@zuzak/owo来尝试实现这一点。 My current code looks like this:我当前的代码如下所示:

const owo = require('@zuzak/owo')
fs = require('fs');

var name = '../jsonfile.json';

var data = fs.readFileSync(name).toString();


fs.writeFileSync('../owo.json', JSON.stringify(owo(data)))

How would I be able to only change the strings, such as "Seaside style. " and not edit any of the string names, such as "81EEA9E6400BFEADE161559AF14EE468" (sorry for any incorrect wording, hopefully you can understand what I am saying.)我如何才能只更改字符串,例如"Seaside style. " ,而不编辑任何字符串名称,例如"81EEA9E6400BFEADE161559AF14EE468" (抱歉,任何不正确的措辞,希望您能理解我在说什么。)

In case you need it, here is the main code used in @zuzak/owo:如果您需要,这里是@zuzak/owo 中使用的主要代码:

const addAffixes = (str) => randomItem(prefixes) + str + randomItem(suffixes)
const substitute = (str) => {
  const replacements = Object.keys(substitutions)
  replacements.forEach((x) => {
    str = replaceString(str, x, substitutions[x])
  })
  return str
}

Use JSON.parse() to parse the JSON file into an object.使用JSON.parse()将 JSON 文件解析为对象。 Then go through the object calling owo() on each value.然后遍历对每个值调用owo()的对象。

var data = JSON.parse(fs.readFileSync(name).toString());
for (let key in data) {
    data[key] = owo(data[key]);
}
fs.writeFileSync("../owo.json", JSON.stringify(data));

Parse the JSON, iterate over keys, modify values.解析 JSON,迭代键,修改值。 If necessary recursively iterate over subobjects too:如有必要,也递归迭代子对象:

function owoifyObject (obj) {
  for (const key in obj) {
    if (typeof obj[key] === 'string') {
      obj[key] = owo(obj[key])
    } else if (typeof obj[key] === 'object' && obj[key]) {
      owoifyObject(obj[key])
    }
  }
}

const data = JSON.parse(fs.readFileSync('file.json').toString())
owoifyObject(data)
fs.writeFileSync('file2.json', JSON.stringify(data, null, 4))

Note that the argument 4 in JSON.stringify is purely there for formatting so that the result looks something like your input did, otherwise you'd get all the data in a single line.请注意, JSON.stringify中的参数4纯粹用于格式化,以便结果看起来像您的输入,否则您将在一行中获得所有数据。

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

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