So I am having an interesting problem with jsStringFormat()
when trying to escape special characters for JSON. I am using the jQuery datatables plugin and doing an AJAX call to Coldfusion.
What appears to be happening is that jsStringFormat() is escaping the apostrophe character and putting \'
in my JSON. According to JSON spec, the single apostrophe doesn't need escaping, thus it breaks.
Here is a sample of my JSON return
{
"sEcho": 2,
"iTotalRecords": 659,
"iTotalDisplayRecords": 201,
"aaData": [
["516", "", "54d 7h 12m", "02- Revenue", "", "Assist in validating error in JCA provided Discount Commission report", "Received", "Work Request", "Jan 1, 2012"],
["616", "", "16d 7h 12m", "02- Revenue", "", "Order/Install new POS Terminal at Katie\'s Workstation", "In Progress", "Work Request", "Oct 31, 2011"],
["617", "", "15d 7h 12m", "02- Revenue", "", "Replace #6081 POS Printer at Kim\'s Desk", "Received", "Work Request", "Oct 31, 2011"]
]
}
You can clearly see the \'
inserted in the descriptions.
I really need to find a way to prevent jsStringFormat()
from escaping the apostrophe.
So far, have this code for attempting to populate the aaData array. Right now I am getting nothing but commas so I know its looping properly, but not populating the data in the right places.
All of this is based off of datatables coldfusion datasource code http://www.datatables.net/development/server-side/coldfusion
<cfcontent reset="Yes" />
<cfset aaData = [] />
<cfset datasetRecords = [] />
<cfloop query="qFiltered" startrow="#val(url.iDisplayStart+1)#" endrow="#val(url.iDisplayLength)#">
<cfif currentRow gt (url.iDisplayStart+1)>,</cfif>
<cfloop list="#listColumns#" index="thisColumn">
<cfif thisColumn neq listFirst(listColumns)>,</cfif>
<cfif thisColumn is "version">
<cfif version eq 0>"-"
<cfelse><cfset datasetData["#version#"] />
</cfif>
<cfelse><cfset datasetData[""] = qFiltered[thisColumn][qFiltered.currentRow] />
</cfif>
<cfset ArrayAppend(datasetRecords, datasetData ) />
</cfloop>
<cfset ArrayAppend(datasetRecords, aaData ) />
</cfloop>
<cfset record = {} />
<cfset record["sEcho"] = val(url.sEcho) />
<cfset record["iTotalRecords"] = qCount.total />
<cfset record["iTotalDisplayRecords"] = qFiltered.recordCount />
<cfset record["aaData"] = aaData />
<cfoutput><cfdump var="#record#"></cfoutput>
<cfoutput>#serializeJSON(record)#</cfoutput>
The JavaScript string I build to execute on the HTMLViewer includes apostrophes to enclose the JSON which uses double quotes for the items. (The data for the form is entered in another application.
JSON syntax is derived from JavaScript object notation syntax: Data is in name/value pairs. Data is separated by commas. Curly braces hold objects.
In JSON, string values must be written with double quotes: Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. You will learn how to convert JavaScript objects into JSON later in this tutorial.
JSON Uses JavaScript Syntax. Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. With JavaScript you can create an object and assign data to it, like this: var person = { name: "John", age: 31, city: "New York" };
JSStringFormat
is designed for escaping data for inclusion in JavaScript, not JSON. In JavaScript, a single quote is a character that needs to be escaped.
On the other hand, SerializeJSON
is actually meant to output JSON, and complies with the JSON spec.
i ran into the same issue with datatables. what i did to fix it was create the jsstringformat string and then do a replace on the returned string to remove all instances of \' with '.
<cfset thisColumnString = jsStringFormat(trim((qFiltered[thisColumn][qFiltered.currentRow])))>
<cfset thisColumnString = replacenocase(thisColumnString,"\'","'","all")>
"#thisColumnString#"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With