简体   繁体   English

ColdFusion 9 JSON 解析错误?

[英]ColdFusion 9 JSON parse error?

I have version of the code that works fine in ColdFusion 10 but ColdFusion 9 I'm getting this error:我有在 ColdFusion 10 中运行良好的代码版本,但在 ColdFusion 9 中我收到此错误:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 13714 of the JSON data

Here is my cffunction where my data is converted to JSON:这是我的数据转换为 JSON 的 cffunction:

      <cffunction name="getBuildings" access="remote" output="true" returnformat="JSON">
                <cfset fnResults = structNew()>

                <cfquery name="getBldg" datasource="myData">
                    SELECT
                        LTRIM(RTRIM(number)) AS bldgNum, 
                        LTRIM(RTRIM(name)) AS bldgName
                    FROM Bldg WITH (NOLOCK)
                    ORDER BY name
                </cfquery>

                <cfset fnResults.recordcount = getBldg.recordcount>

                <cfif getBldg.recordcount EQ 0>
                    <cfset fnResults.message = "No Buildings found.">
                <cfelse>
                    <cfloop query="getBldg">
                        <cfset fnRecBldg[currentRow] = StructNew()>
                        <cfset fnRecBldg[currentRow].bldgName = URLEncodedFormat(getBldg.name)>
                        <cfset fnRecBldg[currentRow].bldgNumber = getBldg.number>
                    </cfloop>
                    <cfset fnResults.data = fnRecBldg>
                </cfif>

                <cfset fnResults.status = "200">
          <cfreturn fnResults>
     </cffunction>

Here is my JQUERY:这是我的 JQUERY:

function getBldg(){
    var dist = $('.chBldg');

        $.ajax({
            type: 'POST',
            url: 'Application.cfc?method=getBuildings',
            data: {},
            dataType: 'json'
        }).done(function(obj){
            var numRecs = obj.RECORDCOUNT;

            if(obj.STATUS == 200){
                if(numRecs == 0){
                    dist.find("option:gt(0)").remove();
                }else{
                    dist.find("option:gt(0)").remove();

                    for(var i=0; i < numRecs; i++){
                        var jsRec = obj.DATA[i];
                        dist.append($("<option />").val(jsRec.BLDGNUMBER).text(decodeURIComponent(jsRec.BLDGNAME) +' ('+ jsRec.BLDGNUMBER +')'));
                    }
                }
            }else{
            }
        }).fail(function(jqXHR, textStatus, errorThrown){
            alert(errorThrown);
        });
    }
}

Here is small example of rerurned data:这是重新生成的数据的小例子:

{"RECORDCOUNT":4,"STATUS":200,"DATA":[
    {"BLDGNAME":"Fall%2DCasey","BLDGNUMBER":"0012"},
    {"BLDGNAME":"Autmun","BLDGNUMBER":"0022"},
    {"BLDGNAME":"Cedar","BLDGNUMBER":0201},
    {"BLDGNAME":"Great%20Lake","BLDGNUMBER":1215}]}

This error must be related to ColdFusion version 9/10 and the way my JSON data is organized.此错误必须与 ColdFusion 版本 9/10 以及我的 JSON 数据的组织方式有关。 I still didn't find the bug.我还是没找到bug。 If anyone see where my code id breaking please let me know.如果有人看到我的代码 ID 在哪里损坏,请告诉我。

I don't think there is anything wrong with your code, there is just lots wrong with serializeJSON in ColdFusion.我不认为你的代码有什么问题,ColdFusion 中的serializeJSON有很多问题。

One trick / hack to force CF9 to treat all numeric-ish values as strings is to append non numeric text to the value and strip it off before using it client side.强制 CF9 将所有数字值视为字符串的一个技巧/技巧是将非数字文本附加到该值并在使用客户端之前将其剥离。 I have used the ASCII control character 2, "Start of Text", in the past.我过去使用过 ASCII 控制字符 2,“文本开始”。 I like using a control character over other text for I feel safe that my users wouldn't intentionally use it.我喜欢在其他文本上使用控制字符,因为我觉得我的用户不会故意使用它是安全的。

To append the control character to your building number, just add chr(2) & before getBldg.number .要将控制字符附加到您的建筑编号,只需在getBldg.number之前添加chr(2) & On your client side, you can instruct jQuery to remove the control characters from the JSON string with the dataFilter property.在您的客户端,您可以使用 dataFilter 属性指示 jQuery 从 JSON 字符串中删除控制字符。

$.ajax({
    type: 'POST',
    url: 'Application.cfc?method=getBuildings',
    data: {},
    dataType: 'json',
    dataFilter: function(data, type){
        //Remove all start of text characters
        return data.replace(/\u0002/g,"");
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM