简体   繁体   English

如何从 JSON.stringify 中删除转义序列以使其可读?

[英]How can I remove escape sequences from JSON.stringify so that it's human-readable?

When I call JSON.stringify() on a complex object in JavaScript, it produces a string with lots of escape sequences (\\", \\\\", etc.).当我在 JavaScript 中对复杂对象调用 JSON.stringify() 时,它会生成一个带有大量转义序列(\\"、\\\\" 等)的字符串。

How can I make it create a human-readable JSON string instead?我怎样才能让它创建一个人类可读的 JSON 字符串呢? Ie, one that you can copy/paste into one of the many JSON validators on the web?即,您可以将其复制/粘贴到网络上的众多 JSON 验证器之一中吗?

To clarify, my #1 concern is removing the escape sequences.澄清一下,我的第一个问题是删除转义序列。

You can use the replacer.您可以使用替换器。 The second parameter provided by JSON.stringify.Replacer could be a function or array. JSON.stringify.Replacer 提供的第二个参数可以是函数或数组。

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() .有关替换器的更多信息,请查看 MDN 页面JSON.stringify()

You can use formatting on JSON.stringify .您可以在JSON.stringify上使用格式。

'\\t' represent a tab character '\\t'代表制表

JSON.stringify({ uno: 1, dos: 2 }, null, '\t');
// returns the string:
// '{
//     "uno": 1,
//     "dos": 2
// }'

ECMAScript 2021, the 12th edition ECMAScript 2021,第 12 版

You can use您可以使用

JSON.stringify() and replaceAll() JSON.stringify()replaceAll()

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll来源: 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>');
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. Space:一个 String 或 Number 对象,用于在输出 JSON 字符串中插入空格以提高可读性。

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.替换器:一个改变字符串化过程行为的函数,或者一个 String 和 Number 对象数组,用作选择要包含在 JSON 字符串中的值对象的属性的白名单。 If this value is null or not provided, all properties of the object are included in the resulting JSON string.如果此值为 null 或未提供,则对象的所有属性都包含在生成的 JSON 字符串中。

So you can do所以你可以做

 var x = {"name":"void", "type":"O'\\"Rielly"}; document.write(JSON.stringify(x, null, ' '));

I would use JSON.stringify() ,我会使用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. JSON.stringify()方法将 JavaScript 值转换为 JSON 字符串,如果指定了替换器函数,则可以选择替换值,或者如果指定了替换器数组,则可以选择仅包含指定的属性。

with four spaces and an appropriate display environment with <pre>...</pre> .有四个空格和适当的显示环境<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>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM