【问题标题】:Is there a way to escape pound character (#) in ColdFusion?有没有办法在 ColdFusion 中转义磅字符 (#)?
【发布时间】:2017-05-18 02:47:14
【问题描述】:

我在 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>

【问题讨论】:

  • 旁注,请务必var/local所有函数局部变量,否则在某些情况下,您可能会遇到奇怪(且难以重现)的错误。

标签: json coldfusion


【解决方案1】:

与您通常逃避磅符号的方式相同:将它们加倍。来自documentation

...要在字符串中包含 [井号],请将字符加倍;为了 例如,使用## 表示单个# 字符。

由于输入是字符串,只需执行 Replace() 即可将单个井号替换为两个井号。

【讨论】:

  • 这是一个 ColdFusion 函数 Replace(),如果是,该函数的完整语法是什么?
  • 你看过上面的链接了吗?它解释了函数和语法。
  • 我明白了,我错过了。现在很清楚了。我明天将更改代码并在此处更新以供以后阅读此内容的人使用
  • 如果您认为它对其他人有帮助,请随时在您的工作示例中发布“答案”。
  • @DoArNa 更好的是,使用解决方案代码更新您的问题并将此答案标记为已接受的答案,以便 Leigh 获得代表点。
【解决方案2】:
 <cfset test = '[{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail Line For Call Box #54940","label4":"test","hcats":"18","freshStart":"0","phoneCode":"","hproduct":"3","checked":false},{"phone":"1001106672","lineType":"Outbound","label1":"Voicemail Line For Call Box #54940","label4":"test","hcats":"54","freshStart":"0","phoneCode":"","hproduct":"--","checked":false}]'>

 <!--- Escaping # sign using replace function so it doesn't get confused with the variable sign in 
ColdFusionldFusion --->

<cfset test = Replace(test, "#", "##" "all")
<cfset dnisObject = DESERIALIZEJSON(test)/>
<cfdump var="#dnisObject#">

【讨论】:

  • 函数表达式中的井号需要转义:Replace(test, "##", "####" "all")。 (在第二个 cfset 之后也缺少关闭 &gt;。)
猜你喜欢
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2014-06-02
相关资源
最近更新 更多