Lấy json của bạn và .stringify[]
nó. Sau đó sử dụng phương pháp .replace[]
và thay thế tất cả các lần xuất hiện của \n
bằng \\n
.
EDIT:
Theo như tôi biết, không có thư viện JS nổi tiếng để thoát khỏi tất cả các ký tự đặc biệt trong một chuỗi. Nhưng, bạn có thể xâu chuỗi phương thức .replace[]
và thay thế tất cả các ký tự đặc biệt như thế này:
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.
Nhưng điều đó khá khó chịu, phải không? Nhập vẻ đẹp của các chức năng, trong đó chúng cho phép bạn chia mã thành
các mảnh và giữ cho luồng chính của tập lệnh của bạn sạch sẽ và miễn phí 8 cuộc gọi .replace[]
được chuỗi. Vì vậy, chúng ta hãy đặt chức năng đó vào một hàm gọi là, escapeSpecialChars[]
. Chúng ta hãy tiếp tục và gắn nó vào
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"];
};
0 của đối tượng 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"];
};
1, vì vậy chúng ta có thể gọi escapeSpecialChars[]
trực tiếp trên các đối tượng chuỗi.Như vậy:
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"];
};
Khi chúng tôi đã xác định chức năng đó, cơ thể chính của mã của chúng tôi đơn giản như thế này:
var myJSONString = JSON.stringify[myJSON];
var myEscapedJSONString = myJSONString.escapeSpecialChars[];
// myEscapedJSONString is now ready to be POST'ed to the server
Mục lục
- Escape Chuỗi JSON trong JavaScript
- Sự kết luận
- Bài đăng này có hữu ích không?
Trong bài đăng này, chúng ta sẽ thấy cách thoát khỏi chuỗi JSON chứa các ký tự mới bằng cách sử dụng JavaScript.
Không có thư viện JavaScript nổi tiếng có thể thoát khỏi tất cả các ký tự đặc biệt trong chuỗi.
Không có cách trực tiếp để thoát khỏi chuỗi JSON trong JavaScript.
Tuy nhiên, bạn có thể chuỗi
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"];
};
3 và thay thế tất cả các ký tự đặc biệt bằng cách sử dụng hàm tùy
chỉnh.Đây là một trong những cách để làm điều đó.
varjsonStr=JSON.stringify[jsonStr];jsonStr=JSON.stringify[jsonStr]; varescapedjsonstring = jsonstr.replace [/[\\]/g, '\\\\']]escapedJSONString=jsonStr .replace[/[\\]/g,'\\\\'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\"]/g, '\\\"'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\/]/g,'\\/'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\b]/g,'\\b'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\f]/g,'\\f'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\n]/g,'\\n'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\r]/g,'\\r'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\t]/g, '\\t']; console.log[escapedJSONString];.log[escapedJSONString]; |
Bạn có thể tạo một chức năng tùy chỉnh như EscapespecialcharSinjSonstring [] như dưới đây:
String.prototype.escapeSpecialCharsInJSONString=function[]{.prototype.escapeSpecialCharsInJSONString=function[]{ & nbsp; & nbsp; & nbsp; & nbsp; returnthis.replace [/[\\]/g, '\\\\']returnthis .replace[/[\\]/g,'\\\\'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\"]/g, '\\\"'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\/]/g,'\\/'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\b]/g, '\\b'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\f]/g,'\\f'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\n]/g, '\\n'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\r]/g,'\\r'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\t]/g, '\\t']; };; |
Hãy cùng xem với sự giúp đỡ của ví dụ.
String.prototype.escapeSpecialCharsInJSONString=function[]{.prototype.escapeSpecialCharsInJSONString=function[]{ & nbsp; & nbsp; & nbsp; & nbsp; returnthis.replace [/[\\]/g, '\\\\']returnthis .replace[/[\\]/g,'\\\\'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\"]/g, '\\\"'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\/]/g,'\\/'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\b]/g, '\\b'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\f]/g,'\\f'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\n]/g, '\\n'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\r]/g,'\\r'] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;.replace[/[\t]/g, '\\t']; };; consttext = '{"name": "John \ n", "sinh": "14/12/1989 \ t"}';text='{"name":"John\n", "birth":"14/12/1989\t"}'; constresult=text.escapeSpecialCharsInJSONString[];result=text.escapeSpecialCharsInJSONString[]; console.log[result];.log[result]; |
Đầu ra
{\ "Tên \": \ "John \ n \", \ "Sinh \": \ "14 \/12 \/1989 \ t \"}\"name\":\"John\n\",\"birth\":\"14\/12\/1989\t\"} |
Như bạn có thể thấy, chúng tôi đã thoát thành công ký tự dòng mới và ký tự tab từ chuỗi JSON.
Sự kết luận
Để thoát khỏi chuỗi JSON chứa các ký tự mới, chuỗi
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"];
};
4 với tất cả các ký tự đặc biệt cần thiết mà bạn muốn thoát.Đó là tất cả về cách thoát khỏi chuỗi JSON chứa các ký tự mới trong JavaScript.