How to keep the spaces in the object when js converts strings?

var params = {"type": "1", 
              "value": "2"}
var str  = JSON.stringify(params);

str outputs"{"type": "1", "value": "2"}", and the original space is removed;
if you want to keep the space after the colon, output"{"type": "1", "value": "2"}", is there any good way besides the repalce method?

Sep.16,2021

JSON.stringify(params, '', 1);


try to see if the JSON transferred out in this way is what you want. You define the object, and it is impossible to leave spaces directly unless you directly define the JSON string

.

JSON.stringify can receive three parameters

JSON.stringify(value[, replacer[, space]])

it is impossible to keep spaces. The spaces in json are meaningless. Any compression tool will drop these Filter

.

but it can have an effect similar to formatting code

for example,

JSON.stringify(data,null,'\t');
//

JSON.stringify(data,null,2);
//2
Menu