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

前端 未结 4 2100
走了就别回头了
走了就别回头了 2020-12-10 17:10

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 h

4条回答
  •  一生所求
    2020-12-10 17:35

    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

    ...
    .

    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('
    ' + JSON.stringify(object, 0, 4) + '
    ');

提交回复
热议问题