JSON response format with \

I am trying to format the json response as such:

[
{
    "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"
}

      

]

but according to JSONLint , \ breaks the "structure"? If I replace \ with | it works so i know the problem is \. I am using the answer in jQuery Autocomplete .

Should I use SerializeJSON () instead? If so, do I need to change something in the autocomplete ajax script?

$(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 );
                }
            });
        }
    });
});

      

+3


source to share


3 answers


\

is an escape character and must be escaped itself if it is part of the content.

So, the line JSON

should like it before the client receives it:



[
    {
        "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"
    }
]

      

+8


source


Have you tried avoiding the backslash?



{
"id": "23030",
"label": "F:\\path\\to\\file\\filename.ext",
"value": "filename.txt"
}

      

+8


source


While other respondents have pointed out that you should avoid backslashes, if you will be using serializeJSON () it will take care of this speedup for you.

+4


source







All Articles