使用\格式化JSON响应

HPW*_*PWD 3 ajax coldfusion json jquery-autocomplete

我正在尝试格式化json响应:

[
{
    "id": "23029",
    "label": "F:\path\to\file\filename.txt",
    "value": "filename.txt"
},
{
    "id": "23030",
    "label": "F:\path\to\file\filename.txt",
    "value": "filename.txt"
},
{
    "id": "23031",
    "label": "F:\path\to\file\filename.txt",
    "value": "filename.txt"
}
Run Code Online (Sandbox Code Playgroud)

]

但根据JSONLint的说法,"正在打破""结构"?如果我用\替换\ 它工作,所以我知道\是问题.我在jQuery的自动完成中使用响应.

我应该使用SerializeJSON()吗?如果是这样,我是否需要在ajax自动完成脚本中更改某些内容?

$(function() {
    var cache = {},
        lastXhr;
    $( "#media" ).autocomplete({
        minLength: 2,
        source: function( request, response ) {
            var term = request.term;
            if ( term in cache ) {
                response( cache[ term ] );
                return;
            }

            lastXhr = $.getJSON( "ajax/search.cfm", request, function( data, status, xhr ) {
                cache[ term ] = data;
                if ( xhr === lastXhr ) {
                    response( data );
                }
            });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

xan*_*ded 8

\是转义字符,如果它是内容的一部分,则需要自行转义.

所以,在JSON客户端收到它之前,字符串应该是这样的:

[
    {
        "id": "23029",
        "label": "F:\\path\\to\\file\\filename.txt",
        "value": "filename.txt"
    },
    {
        "id": "23030",
        "label": "F:\\path\\to\\file\\filename.txt",
        "value": "filename.txt"
    },
    {
        "id": "23031",
        "label": "F:\\path\\to\\file\\filename.txt",
        "value": "filename.txt"
    }
]
Run Code Online (Sandbox Code Playgroud)


p0r*_*ter 8

你试图逃避反斜杠吗?

{
"id": "23030",
"label": "F:\\path\\to\\file\\filename.ext",
"value": "filename.txt"
}
Run Code Online (Sandbox Code Playgroud)