jsStringFormat()和带有JSON的撇号 [英] jsStringFormat() and apostrophe with JSON

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

问题描述

因此,当尝试为JSON转义特殊字符时,我遇到了一个有趣的问题与 jsStringFormat()。我使用jQuery数据表插件,并做一个AJAX调用Coldfusion。

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.

我真的需要找到一种方法来防止 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.

所有这些都是基于数据表coldfusion数据源代码 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天全站免登陆