简体   繁体   English

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

[英]JSON breaking: using coldfusion regex to remove some quotes and double quotes

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 但无法修复,请指导我谢谢

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. 我不确定ColdFusion正则表达式或任何正则表达式风格是否能胜任该任务。 Rather, whatever service is producing the invalid JSON needs to be fixed. 而是,无论产生无效JSON的任何服务都需要修复。

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

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