JSON破解:使用coldfusion正则表达式删除一些引号和双引号

时间:2022-06-02 00:24:14

i am going nuts here, trying to remove some quotes and double quotes in my json response,

我在这里疯了,试图在我的json响应中删除一些引号和双引号,

there are some characters too like period, comma etc, i am trying like this

还有一些字符也像句号,逗号等,我正在尝试这样

<cfset mystring = rereplace(mystring, '(['""])', '\\\1', 'all') /> 

but unable to fix it, please guide me thanks

但无法修复它,请指导我谢谢

1 个解决方案

#1


1  

I think the problem is that you're enclosing your regex pattern string in single quotes, but then escaping the double quote inside that string, but not the single quote. You might try the following:

我认为问题是你将你的正则表达式模式字符串用单引号括起来,然后转义该字符串中的双引号,而不是单引号。您可以尝试以下方法:

<cfset mystring = rereplace(mystring, "(['""])", "\\\1", "all") />

But I'm not sure that will actually do what you want. That will also escape double and single quotes where they don't need to be escaped -- such as the quotes surrounding names and values. For example, the JSON

但我不确定它会不会真正做你想要的。这也将逃避双引号和单引号,它们不需要转义 - 例如名称和值周围的引号。例如,JSON

[{"name":"value"}]

would become

[{\"name\":\"value\"}]

Surely that isn't what you want! Rather, you would need to determine where double quotes fall within strings surrounded by double quotes, and escape those (assuming they're not already escaped). I am not certain that ColdFusion regex, or any regex flavor, is up to that task. Rather, whatever service is producing the invalid JSON needs to be fixed.

当然这不是你想要的!相反,您需要确定双引号在双引号括起的字符串中的位置,并将其转义(假设它们尚未转义)。我不确定ColdFusion正则表达式,或任何正则表达式的风格,都取决于该任务。相反,无论什么服务产生无效的JSON都需要修复。

#1


1  

I think the problem is that you're enclosing your regex pattern string in single quotes, but then escaping the double quote inside that string, but not the single quote. You might try the following:

我认为问题是你将你的正则表达式模式字符串用单引号括起来,然后转义该字符串中的双引号,而不是单引号。您可以尝试以下方法:

<cfset mystring = rereplace(mystring, "(['""])", "\\\1", "all") />

But I'm not sure that will actually do what you want. That will also escape double and single quotes where they don't need to be escaped -- such as the quotes surrounding names and values. For example, the JSON

但我不确定它会不会真正做你想要的。这也将逃避双引号和单引号,它们不需要转义 - 例如名称和值周围的引号。例如,JSON

[{"name":"value"}]

would become

[{\"name\":\"value\"}]

Surely that isn't what you want! Rather, you would need to determine where double quotes fall within strings surrounded by double quotes, and escape those (assuming they're not already escaped). I am not certain that ColdFusion regex, or any regex flavor, is up to that task. Rather, whatever service is producing the invalid JSON needs to be fixed.

当然这不是你想要的!相反,您需要确定双引号在双引号括起的字符串中的位置,并将其转义(假设它们尚未转义)。我不确定ColdFusion正则表达式,或任何正则表达式的风格,都取决于该任务。相反,无论什么服务产生无效的JSON都需要修复。