admin管理员组文章数量:1178539
When I call JSON.stringify() on a complex object in JavaScript, it produces a string with lots of escape sequences (\", \\", etc.).
How can I make it create a human-readable JSON string instead? I.e., one that you can copy/paste into one of the many JSON validators on the web?
To clarify, my #1 concern is removing the escape sequences.
When I call JSON.stringify() on a complex object in JavaScript, it produces a string with lots of escape sequences (\", \\", etc.).
How can I make it create a human-readable JSON string instead? I.e., one that you can copy/paste into one of the many JSON validators on the web?
To clarify, my #1 concern is removing the escape sequences.
Share Improve this question edited Mar 11, 2017 at 13:51 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Jan 7, 2016 at 6:46 INBINB 1751 gold badge1 silver badge6 bronze badges 1- are you using jquery or other framework? – Peter P. Commented Jan 7, 2016 at 6:47
5 Answers
Reset to default 13You can use the replacer. The second parameter provided by JSON.stringify.Replacer could be a function or array.
In your case we can create a function which replaces all the special characters with a blank space.The below example replaces the whitespaces and underscores.
function replacer(key, value) {
return value.replace(/[^\w\s]/gi, '');
}
var foo = {"a":"1","b":2};
var jsonString = JSON.stringify(foo, replacer);
If you simply want to replace the one special character, use:
JSON.stringify({ a: 1, b: 2 }, null, '\t');
For more information on replacer, check the MDN page JSON.stringify().
ECMAScript 2021, the 12th edition
You can use
JSON.stringify()
and replaceAll()
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
const foo = {
A: 'This is my \\',
B: 'This \\ is his \\'
};
let jsonString = JSON.stringify(foo, null, 2);
document.write(jsonString);
jsonString = jsonString.replaceAll('\\', '');
document.write('<pre>' + jsonString + '</pre>');
You can use formatting on JSON.stringify.
'\t'
represent a tab character
JSON.stringify({ uno: 1, dos: 2 }, null, '\t');
// returns the string:
// '{
// "uno": 1,
// "dos": 2
// }'
JSON.stringify(value[, replacer[, space]])
Space: A String or Number object that's used to insert white space into the output JSON string for readability purposes.
Replacer: A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.
So you can do
var x = {"name":"void", "type":"O'\"Rielly"};
document.write(JSON.stringify(x, null, ' '));
I would use JSON.stringify()
,
The
JSON.stringify()
method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
with four spaces and an appropriate display environment with <pre>...</pre>
.
The result is good, readable, and copyable for other use.
var object = { name: 'void', type1: "O'\"This", type2: 'O\'That', a: [1, 2, 3] };
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');
版权声明:本文标题:javascript - How can I remove escape sequences from JSON.stringify so that it's human-readable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738103175a2064068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论