Quantcast
Channel: Adobe Community: Message List - ColdFusion
Viewing all articles
Browse latest Browse all 21760

Re: Why is ColdFusion adding backslashes to my Concatenated string?

$
0
0

Aegis Kleais wrote:

 

You're not performing a JSStringFormat() on that anywhere, are you?  (maybe in some code not posted).  JSStringFormat() assumes the text passed to it is a literal string, and then it will escape quoted values (like we're seeing here)

 

Also, on an aside, your JSON data is invalid, no?

 

Your loop updates the action list with an array, but it always assumes there is another index of values, so it prepends the comma.  You can even see in the final version your array of data ends with a '...},]}'  You may have to do a conditional test to see whether the loop is at it's max index, and if not, then prepend the comma.

 

Wow... I feel like an idiot. I still don't know why it was doing it.

 

But I looked into what you said about how I was incorrectly structuring my JSON so I changed the function to:

<cffunction name="getActions" output="false" access="remote" returnformat="JSON">    <cfquery name="Actions">      SELECT * FROM Actions    </cfquery>    <cfset ActionsList = '{"success" : "true", "Actions" : ['>    <cfloop query="Actions">      <cfset ActionsList = ActionsList & '{ "ID" : "'& ID & '", "Name" : "'& Name & '"},'>    </cfloop>    <cfset ActionsList = Left(ActionsList, len(ActionsList)-1) & ']}'>    <cfreturn ActionsList>  </cffunction> 

 

The Left() function removes the traling comma that you talked about so that the end of my JSON would properly be "'...}]}

 

I also removed the Bracket before "Actions" so the beginning is '{"success" : "true", "Actions...' Instead of '{"success" : "true"}, {"Actions" : [...'

 

Once I structured my JSON properly ColdFusion removed the backslashes. The above code outputs this:

{"success" : "true", "Actions" : [{ "ID" : "1", "Name" : "View Public"},
{ "ID" : "2", "Name" : "View Full"},{ "ID" : "3", "Name" : "Create Page"},
{ "ID" : "4", "Name" : "Create Part"},{ "ID" : "5", "Name" : "Create Section"},
{ "ID" : "6", "Name" : "Create Question"},{ "ID" : "7", "Name" : "Create Table"},
{ "ID" : "8", "Name" : "Assign Pages to Location"},{ "ID" : "9", "Name" : "Input Data"},
{ "ID" : "10", "Name" : "Edit Page"},{ "ID" : "11", "Name" : "Edit Part"},
{ "ID" : "12", "Name" : "Edit Section"},{ "ID" : "13", "Name" : "Edit Question"},
{ "ID" : "14", "Name" : "Edit Table"},{ "ID" : "15", "Name" : "Assign Pages to Location Type"},
{ "ID" : "16", "Name" : "Add Location"},{ "ID" : "17", "Name" : "Add Location Type"},
{ "ID" : "18", "Name" : "Change Location"},{ "ID" : "19", "Name" : "Assign Location Designee"},
{ "ID" : "20", "Name" : "Assign District Designee"},
{ "ID" : "21", "Name" : "Update User Information"},{ "ID" : "22", "Name" : "Add User Type"},
{ "ID" : "23", "Name" : "Manage Permissions"}]}

 

It seems like coldfusion adds the backslash if the JSON "String" is not created properly...

 

Seems like the fault was mine and not ColdFusions.


Viewing all articles
Browse latest Browse all 21760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>