我正在尝试格式化一个 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"
}
]
但根据 JSONLint ,\正在打破“结构”?如果我用 | 替换\它有效,所以我知道\是问题所在。我正在使用 jQuery's Autocomplete 中的响应.
我应该改用 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 );
}
});
}
});
});
最佳答案
\
是转义字符,如果它是内容的一部分,则需要对其本身进行转义。
所以,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"
}
]
关于ajax - 使用\格式化 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10012780/