我在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>
答案 0 :(得分:9)
你通常会逃脱英镑标志:加倍。来自documentation:
...要在字符串中包含[井号],请将该字符加倍;对于 例如,使用
##
表示单个#
字符。
由于输入是一个字符串,只需用Replace()代替一个带有两个磅符号的单个符号。
答案 1 :(得分: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#">