我必须形成一个JSON字符串,其中一个值有新的行字符。这必须转义,然后使用AJAX调用发布。谁能建议一种用JavaScript转义字符串的方法?我没有使用jQuery。
获取JSON和.stringify()。然后使用.replace()方法将所有出现的\n替换为\\n。
编辑:
据我所知,没有知名的JS库用于转义字符串中的所有特殊字符。但是,你可以链接.replace()方法,像这样替换所有的特殊字符:
var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// myEscapedJSONString is now ready to be POST'ed to the server.
但这很恶心,不是吗?函数的美妙之处在于,它们允许您将代码分解成片段,并保持脚本的主要流程干净,并且没有8个链式的.replace()调用。因此,让我们将该功能放入一个名为escapeSpecialChars()的函数中。让我们继续把它附加到String对象的原型链上,这样我们就可以直接在String对象上调用escapeSpecialChars()。
像这样:
String.prototype.escapeSpecialChars = function() {
return this.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
};
一旦我们定义了这个函数,我们代码的主体就像这样简单:
var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.escapeSpecialChars();
// myEscapedJSONString is now ready to be POST'ed to the server
恕我直言,Alex给出的答案是不正确的,婉转地说:
Alex试图转义的一些字符根本不需要转义(比如&和'); \b根本不是退格字符,而是一个单词边界匹配 需要转义的字符不会被处理。
这个函数
escape = function (str) {
// TODO: escape %x75 4HEXDIG ?? chars
return str
.replace(/[\"]/g, '\\"')
.replace(/[\\]/g, '\\\\')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t')
; };
似乎是一个更好的近似。
根据user667073的建议,除了先重新排序反斜杠替换,并修复引号替换
escape = function (str) {
return str
.replace(/[\\]/g, '\\\\')
.replace(/[\"]/g, '\\\"')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t');
};
在JSON.stringify上还有第二个参数。所以,更优雅的解决方案是:
function escape (key, val) {
if (typeof(val)!="string") return val;
return val
.replace(/[\"]/g, '\\"')
.replace(/[\\]/g, '\\\\')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t')
;
}
var myJSONString = JSON.stringify(myJSON,escape);
单引号的一个小更新
function escape (key, val) {
if (typeof(val)!="string") return val;
return val
.replace(/[\\]/g, '\\\\')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t')
.replace(/[\"]/g, '\\"')
.replace(/\\'/g, "\\'");
}
var myJSONString = JSON.stringify(myJSON,escape);
我在我的一个Ajax调用中得到了相同的情况,其中JSON由于Textarea字段中的换行符抛出了一个错误。这里给出的解决方案对我没用。 所以我使用Javascript的.escape函数,它工作得很好。然后,为了从JSON中检索值,我使用.unescape进行了反转义。
这是一个老问题,但这个解决方案对我来说并不是很有效,因为它并没有解决所有的情况。我终于找到了一个答案
我将发布使用escape和encode uri组件的组合解决方案:
// implement JSON stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
var rawString = (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
return rawString;
}
};
function escape (key, val) {
if (typeof(val)!="string") return val;
var replaced = encodeURIComponent(val);
return replaced;
}
JSON.stringifyEscaped = function(obj){
return JSON.stringify(obj,escape);
}
在使用任何形式的Ajax时,Web上似乎缺乏关于从CGI服务器接收到的响应格式的详细文档。这里的一些条目指出,必须转义返回文本或json数据中的换行符,以防止json转换中的无限循环(挂起)(可能是通过抛出未捕获的异常创建的),无论是由jQuery自动完成还是使用Javascript系统或库json解析调用手动完成。
In each case where programmers post this problem, inadequate solutions are presented (most often replacing \n by \\n on the sending side) and the matter is dropped. Their inadequacy is revealed when passing string values that accidentally embed control escape sequences, such as Windows pathnames. An example is "C:\Chris\Roberts.php", which contains the control characters ^c and ^r, which can cause JSON conversion of the string {"file":"C:\Chris\Roberts.php"} to loop forever. One way of generating such values is deliberately to attempt to pass PHP warning and error messages from server to client, a reasonable idea.
根据定义,Ajax在幕后使用HTTP连接。这样的连接使用GET和POST传递数据,这两者都需要对发送的数据进行编码,以避免错误的语法,包括控制字符。
这给了构造解决方案的足够提示(它需要更多的测试):在PHP(发送)端使用rawurlencode来编码数据,在Javascript(接收)端使用unescape来解码数据。在某些情况下,您将把它们应用于整个文本字符串,在其他情况下,您将只应用于JSON中的值。
如果这个想法被证明是正确的,那么可以构造一些简单的例子来帮助各个级别的程序员一劳永逸地解决这个问题。
如果您的服务器端脚本是PHP,请使用json_encode(), Json_encode()转义换行符和其他意想不到的标记 (如果不使用PHP,请为您的脚本语言寻找类似的函数)
然后在JavaScript中使用$.parseJSON(),完成!
这是一个旧帖子。但对于那些使用angular.fromJson和JSON.stringify的人来说,它仍然是有帮助的。 已弃用Escape()。 用这个代替,
var uri_enc = encodeURIComponent(uri); //before you post the contents
var uri_dec = decodeURIComponent(uri_enc); //before you display/use the contents.
ref http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp
我使用内置的jQuery.serialize()从文本区域提取值以urlencode输入。专业的部分是,你不需要搜索替换每一个特殊的字符在你自己和我也保持换行符和转义html。为了序列化工作,似乎输入字段需要有一个name属性,但它也添加了相同的属性转义字符串,需要被替换掉。也许不是你想要的,但对我有用。
var myinputfield = jQuery("#myinputfield");
var text = myinputfield.serialize();
text = text.replace(myinputfield.attr('name') + '=','');
像你一样,我一直在寻找一些评论和帖子来替换我的JSON中包含html对象的特殊转义字符。
我的对象是删除JSON对象中的特殊字符,并渲染JSON对象内部的html。
以下是我所做的,希望使用起来非常简单。
首先我使用了JSON。stringify我的json对象和json。解析结果。
如:
JSON.parse(JSON.stringify(jsonObject));
它解决了我的问题,并使用纯Javascript。
看起来这真的是一个古老的帖子:-)但是伙计们,我对此最好的解决办法,是100%的工作,没有复杂的代码,是使用编码/解码到base64的两个函数。它们是atob()和btoa()。到目前为止,最简单和最好的方法,没有必要担心如果你错过了任何字符转义。
乔治
使用encodeURIComponent()对字符串进行编码。
例如。var myEscapedJSONString = encodeURIComponent(JSON.stringify(myJSON));
你不需要解码它,因为web服务器会自动做同样的事情。
EDIT: Check if the api you’re interacting with is set to Content-Type: application/json, &/or if your client http library is both stringify-ing and parsing the http request body behind the scenes. My client library was generated by swagger, and was the reason I needed to apply these hacks, as the client library was stringifying my pre-stringified body (body: “jsonString”, instead of body: { ...normal payload }). All I had to do was change the api to Content-Type: text/plain, which removed the JSON stringify/parsing on that route, and then none of these hacks were needed. You can also change only the "consumes" or "produces" portion of the api, see here.
原文:如果你的谷歌一直登陆你这里,你的api抛出错误,除非你的JSON双引号转义("{\"foo\": true}"),所有你需要做的是stringify两次,例如JSON.stringify(JSON.stringify(bar)))
RegExp单行,使用这个函数:
function escapeJSON(string) {
return string.replace(/[\n"\&\r\t\b\f]/g, '\\$&');
}
推荐文章
- 清除JavaScript中的缓存
- 如何在使用Javascript替换DOM元素?
- 在Redux应用程序中哪里写localStorage ?
- 如何在ReactJS中从“外部”访问组件方法?
- 为时刻添加持续时间(moment.js)
- 如何在JavaScript中获得时区名称?
- 在JSON键名中哪些字符是有效的/无效的?
- jQuery中的live()转换为on()
- 如何区分鼠标的“点击”和“拖动”
- IE9是否支持console.log,它是一个真实的功能吗?
- Node.js同步执行系统命令
- 如何转义JSON字符串包含换行字符使用JavaScript?
- jQuery等价于JavaScript的addEventListener方法
- jQuery需要避免的陷阱
- JavaScript中变量字符串的XML解析