gpt4 book ai didi

javascript - 使用正则表达式删除引号内的引号

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

我有一个 json 文件,其中的值中有很多双引号。json文件差不多有27000条记录。

我想删除或替换值中的双引号,否则它不会被接受为一个好的 json 文件。我该怎么做?

问题是值内有一个双引号的记录,但也有多个记录在值内。

除了替换或删除引号,还可以删除整个键和值。反正我是不会用的。这样做更容易吗?

这是 json 文件中 1 条记录的示例:

 {
"adlibJSON": {
"recordList": {
"record": [
{
"@attributes": {
"priref": "4372",
"created": "2011-12-09T23:09:57",
"modification": "2012-08-11T17:07:51",
"selected": "False"
},
"acquisition.date": [
"1954"
],
"documentation.title": [
"A lot of text with a lot of extra double quotes like "this" and "this""
] ... ...

问题出在键的值上:document.title。我有 sublime text 2,我用它来查找和替换。

最佳答案

有一种方法,但为了做到这一点,您必须确保可以对数据做出以下假设:

  • “documentation.title”在用作键时只能在您的数据中出现一次。
  • “documentation.title”引用的数组值应该只有一个元素。
  • 字符“]”不应出现在值中。

然后您将按照这些步骤操作:

/* find first index of "[" after "documentation.title" */
n = s.indexOf("[", s.indexOf('"documentation.title"'));

/* Find index of closing "]" */
n2 = s.indexOf("]", n);

/* Get the substring enclosed by these indexes */
x = s.substr(n+1, n2-n-1);

/* Remove every double quotes in this string and rebuild the original string with the corrected value. */
s.substr(0, n) + '["' + x.replace(/"/g, "") + '"]' + s.substr(n2+1);

编辑:如果您对保留更正后的值本身不感兴趣,可以将其替换为空字符串。

关于javascript - 使用正则表达式删除引号内的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584128/

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