我在 CFC 文件中有一个函数,它采用 JSON 字符串作为参数。然后该函数使用反序列化的数据来执行 UPDATE 查询。在 JSON 字符串中,其中一个属性的名称包含字符 #
。该字符使代码在 ColdFusion 中中断,因为它被解释为变量。有没有办法让 ColdFusion “转义”该字符并将其视为字符串?请记住,这是 JSON 字符串的一部分。
下面是我的功能。由于 JSON 字符串中存在 #
字符,它不允许我访问 dnisObject
。如果我从 JSON 字符串中删除那些 #
,它就可以正常工作。这些值必须使用 #
存储在数据库中,因此我不能完全删除它们。
<cffunction name="updateDnisHproduct" access="remote">
<cfargument name = "lid" type = "numeric" required = "yes">
<cfargument name = "updatedObj" type = "string" required="yes">
<cfset dnisObject = DESERIALIZEJSON(arguments.updatedObj)/>
<cfset test =[{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail for line #54940","label4":"test","hcat":"18","freshStart":"0","phoneCode":"","hproduct":"3","checked":false},{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail Line Box #58940","label4":"12","hcat":"54","freshStart":"0","phoneCode":"","hproduct":"12","checked":false}'>
<cfset dnisObject = DESERIALIZEJSON(test)/>
</cffunction>
最佳答案
与通常逃避英镑符号的方式相同:将它们加倍。来自 documentation :
...To include [pound signs] in a string, double the character; for example, use
##
to represent a single#
character.
由于输入是字符串,所以只需执行 Replace()用两个井号代替单个井号。
关于json - 有没有办法在 ColdFusion 中转义井号字符 (#)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44037766/