Is there a way to avoid the pound symbol (#) in ColdFusion?

I have a function in a CFC file that takes a JSON string as an argument. The function then uses the deserialized data to execute the UPDATE query. In a JSON string, one of the properties has a character #as part of the name. This character causes code to break in ColdFusion because it is interpreted as a variable. Is there a way to make ColdFusion "escape" from this character and consider it just a string? Remember that this is part of the JSON string.

Below is my function. This does not allow me to access dnisObjectdue to characters #in the JSON string. If I remove those #from the JSON string, it works fine. These values ​​should be stored in the database using #, so I cannot just delete them completely.

<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>
+4
source share
2 answers

In the same way, you usually avoid pound signs: double them. From the doc:

... To include [pound signs] in the string, double the character; For example, use ##to represent a single character #.

Since the input is a string, simply execute Replace () to replace one pound with two pounds.

+9
source
 <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#">
+1

Source: https://habr.com/ru/post/1677410/


All Articles