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

前端 未结 4 2107
走了就别回头了
走了就别回头了 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条回答
  •  猫巷女王i
    2020-12-10 17:35

    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, ' '));

提交回复
热议问题