format JSON response with \

I'm trying to format a 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, the \ is breaking the "structure"? If I replace the \ with a | it works so I know the \ is the problem. I'm using the response in jQuery's Autocomplete.

Should I be using SerializeJSON() instead? If so, do I need to change something in the ajax autocomplete 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 );
                }
            });
        }
    });
});

Answers


The \ is the escape character and needs to be escaped itself if it is part of the content.

So, the JSON string should like this 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"
    }
]

have you tried to escape the backslash?

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

Need Your Help

Java socket polling for checking if client is available

java sockets exception java-io bufferedwriter

I am trying to poll a socket on server side to check if client is still available. I checked a few threads here but nothing seems to work.

Alternate fonts for console windows?

fonts console terminal

I like to find some new/interesting fixed width fonts for console.