.json文件中包含以下内容:
{
"WRATH OF THE C’TAN": {
"description": "The C’tan’s ever-burning rage at their enslavement is turned upon the enemy with cataclysmic results.",
"stratagem": "Use this Stratagem after a C’TAN SHARD from your army has resolved a Power of the C’tan. Roll a D6 to randomly select a Power of the C’tan from page 113. The C’tan Shard immediately uses the power rolled, even if it has already used that power this phase.",
"cost": 2,
"conditions": [
"factionkeywords"
],
"factionkeywords": [
"C'tan Shards"
]
}
}
我正在使用以下代码读取JSON文件并在Node上运行它:
var fileContents = fs.readFileSync(path, "utf8").toString();
library = JSON.parse(fileContents);
我正在迭代从JSON文件读取的factionionwordwords数组,并将每个字符串(在这种情况下,一个字符串,还有一些其他字符串)与一个单元进行比较,以查看其factionkeywords是否包含该字符串。
我遇到的问题是factionionkeywords数组中的字符串转义了撇号。当我console.log对象时,我得到了:
{
description: 'The powers of the C’tan are myriad and terrifying. To battle a fragment of these shackled star gods is to find reality unbound.',
stratagem: 'Use this Stratagem at the start of your Movement phase. Select a C’TAN SHARD from your army. That model can replace one of its Powers of the C’tan with a different Power of the C’tan of your choice.',
cost: 1,
conditions: [ 'factionkeywords' ],
factionkeywords: [ 'C\'tan Shards' ]
}
我不明白为什么撇号在数组字符串中而不是描述字符串或策略字符串中被转义。结果,我无法检查一个单元是否包含此字符串,因为它将不匹配。
我已经做了很多Google搜索,但是什么也找不到。出现的所有内容都是如何添加转义符。我想知道为什么它会在导入时自动转义撇号,而只在数组而不是其他字符串中转义。
提前谢谢!
答案 0 :(得分:3)
console.log
添加了转义符,以免将单引号与单引号和结束引号引起混淆,因为单引号使用相同的字符。它在您的字符串中不存在。尝试console.log(myobject.factionkeywords[0])
来查看字符串的真实含义。
在description
上未进行转义的原因是因为您那里没有相同的字符。 factionkeywords
包含'APOSTROPHE'(U + 0027),而description
包含'RIGHT SINGLE QUOTATION MARK'(U + 2019)。