jsStringFormat()和撇号与JSON [英] jsStringFormat() and apostrophe with JSON

查看:125
本文介绍了jsStringFormat()和撇号与JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当尝试转义JSON的特殊字符时,我有一个有趣的问题, jsStringFormat()。我正在使用jQuery数据表插件,并对Coldfusion进行AJAX调用。

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.

似乎发生的是jsStringFormat()正在转义撇号字符,并将 \'在我的JSON。根据JSON规范,单个撇号不需要转义,因此它会中断。

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.

以下是我的JSON返回的示例

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.

我真的需要找到一种方法来防止code> jsStringFormat()转义撇号。

I really need to find a way to prevent jsStringFormat() from escaping the apostrophe.

到目前为止,填充aaData数组。现在我只得到逗号,所以我知道它的循环正确,但是没有在正确的地方填充数据。

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.

所有这一切都是基于数据库的冷灌装数据源代码 http://www.datatables.net/development/server-side/coldfusion

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>


推荐答案

JSStringFormat 旨在转义数据以便包含在JavaScript 中,而不是JSON。在JavaScript中,单引号是需要转义的字符。

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.

另一方面, SerializeJSON 实际上意味着输出JSON,并符合JSON规范。

On the other hand, SerializeJSON is actually meant to output JSON, and complies with the JSON spec.

这篇关于jsStringFormat()和撇号与JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆