[英]format JSON response with \
我正在尝试格式化 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 的自动完成中的响应。
我应该改用 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"
}
]
你试过逃避反斜杠吗?
{
"id": "23030",
"label": "F:\\path\\to\\file\\filename.ext",
"value": "filename.txt"
}
虽然其他响应者指出反斜杠应该是 escaping,但如果您要使用 serializeJSON(),它会为您处理 escaping。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.