gpt4 book ai didi

javascript - 撇号在 JSON 字符串数组中转义

转载 作者:行者123 更新时间:2023-11-30 11:05:52 26 4
gpt4 key购买 nike

我在 .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 文件中读入的 factionkeywords 数组,并将每个字符串(在本例中为一个,有些则有更多)与一个单元进行比较,以查看其 factionkeywords 是否包含该字符串。

我遇到的问题是 factionkeywords 数组中的字符串正在转义撇号。当我 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' ]
}

我不明白为什么撇号在数组字符串中被转义,但在描述或策略字符串中却没有。因此,我无法检查某个单元是否包含此字符串,因为它不匹配。

我已经做了很多谷歌搜索,但一直找不到任何东西。出现的一切都是如何添加转义。我想知道为什么它会在导入时自动转义撇号,但只是在数组中而不是其他字符串中。

提前致谢!

最佳答案

console.log 添加了转义符,以免将撇号与开始和结束单引号混淆,因为它使用相同的字符。它不存在于您的字符串中。尝试 console.log(myobject.factionkeywords[0]) 以查看您的字符串的真实情况。

没有对 description 进行转义的原因是因为那里没有相同的字符。 factionkeywords 包含“撇号”(U+0027),而 description 包含“右单引号”(U+2019)。

关于javascript - 撇号在 JSON 字符串数组中转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644720/

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