JavaScript 中 JSON.stringify 中的第二个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17537571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:56:10  来源:igfitidea点击:

Second argument in JSON.stringify in JavaScript

javascriptjsonfunctionstringify

提问by Blaszard

In JavaScript's JSON.stringify()function, I occasionally see the following syntax:

在 JavaScript 的JSON.stringify()函数中,我偶尔会看到以下语法:

JSON.stringify(obj, null, 4)

However, I can't get what the second argument, null, is supposed to do. As long as I know, the above function takes an object as its first argument, and converts it to a string variable. The third argument, 4in this case, indents and pretty-prints the resultant string object. But I can't see what the second argument tries to do even after I read the explanation on the official document... So what does the argument do? Or is it just there in order to take in the third argument? (But I think then the function should take both argument name and its parameter, such that for example, JSON.stringify(obj, space=4). I'm not sure whether that sort of syntax is allowed in JavaScript, so forgive me if it's not. But I don't know my expectation is correct in the first place, so would like to throw a question anyway).

但是,我无法理解第二个参数 ,null应该做什么。据我所知,上面的函数将一个对象作为它的第一个参数,并将其转换为字符串变量。4在这种情况下,第三个参数缩进并漂亮地打印结果字符串对象。但是即使我阅读了官方文档上的解释,我也看不到第二个参数试图做什么......那么这个参数有什么作用?或者它只是为了接受第三个论点?(但我认为该函数应该同时采用参数名称及其参数,例如,JSON.stringify(obj, space=4)。我不确定 JavaScript 中是否允许这种语法,所以如果不允许,请原谅我。但我不知道我的期望首先是正确的,所以无论如何还是想提出一个问题)。

Thanks.

谢谢。

采纳答案by Andrey Shchekin

There is no way to pass third parameter without passing second parameter in JavaScript.
So nullis a placeholder for replacerfunction when you need to pass space.

在 JavaScript 中,如果不传递第二个参数,就无法传递第三个参数。
所以null是一个占位符replacer的功能,当你需要传递space

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

回答by Paul

The second parameter can be a function to perform replacement while stringifying.

第二个参数可以是在字符串化时执行替换的函数。

A nullor undefined second parameter means that you want to use standard stringification, without any customization.

一个或未定义的第二个参数意味着您要使用标准的字串,无需任何定制。

From https://developer.mozilla.org/en-US/docs/Using_native_JSON#The_replacer_pa

来自https://developer.mozilla.org/en-US/docs/Using_native_JSON#The_replacer_pa

Starting in Firefox 3.5.4, JSON.stringify() offers additional customization capabilities through the use of optional parameters. The syntax is:

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

value The JavaScript object to convert into a JSON string.

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.

space A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used. The replacer parameter

The replacer parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter. Initially it gets called with an empty key representing the object being stringified, and it then gets called for each property on the object or array being stringified. It should return the value that should be added to the JSON string, as follows:

If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string. If you return a String, that string is used as the property's value when adding it to the JSON string. If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string. If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string. If you return undefined, the property is not included in the output JSON string. Note: You cannot use the replacer function to remove values from an array. If you return undefined or a function then null is used instead.

Example

从 Firefox 3.5.4 开始,JSON.stringify() 通过使用可选参数提供额外的自定义功能。语法是:

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

value 要转换为 JSON 字符串的 JavaScript 对象。

替换器 一个改变字符串化过程行为的函数,或者一个 String 和 Number 对象数组,用作选择要包含在 JSON 字符串中的值对象的属性的白名单。如果此值为 null 或未提供,则对象的所有属性都包含在生成的 JSON 字符串中。

space 一个 String 或 Number 对象,用于在输出 JSON 字符串中插入空格以提高可读性。如果这是一个数字,则表示用作空格的空格字符数;如果大于此数字,则此数字的上限为 10。小于 1 的值表示不应使用空格。如果这是一个字符串,则该字符串(或字符串的前 10 个字符,如果它长于该字符)用作空白。如果未提供此参数(或为空),则不使用空格。替换参数

replacer 参数可以是函数或数组。作为一个函数,它需要两个参数,键和值被字符串化。找到键的对象作为替换器的 this 参数提供。最初,它使用一个表示被字符串化的对象的空键被调用,然后被字符串化的对象或数组上的每个属性调用。它应该返回应该添加到 JSON 字符串的值,如下所示:

如果您返回一个数字,则与该数字对应的字符串在添加到 JSON 字符串时用作该属性的值。如果您返回一个字符串,则在将该字符串添加到 JSON 字符串时将该字符串用作该属性的值。如果您返回布尔值,则在将其添加到 JSON 字符串时,“true”或“false”将视情况用作该属性的值。如果您返回任何其他对象,该对象将递归字符串化为 JSON 字符串,对每个属性调用替换函数,除非该对象是一个函数,在这种情况下,不会向 JSON 字符串添加任何内容。如果返回 undefined,则输出 JSON 字符串中不包含该属性。注意:您不能使用 replacer 函数从数组中删除值。如果您返回 undefined 或函数,则使用 null 代替。

例子

function censor(key, value) {
  if (typeof(value) == "string") {
    return undefined;
  }
  return value;
}

var foo = {foundation: "Mozilla", 
           model: "box", 
           week: 45, 
           transport: "car", 
           month: 7};
var jsonString = JSON.stringify(foo, censor);
The resulting JSON string is {"week":45,"month":7}.

If replacer is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string.

如果 replacer 是一个数组,则该数组的值指示应包含在结果 JSON 字符串中的对象中的属性名称。

回答by Asgar Ali Khachay

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

replacerA function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering 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.

替换器改变字符串化过程行为的函数,或用作白名单的 String 和 Number 对象数组,用于选择/过滤要包含在 JSON 字符串中的值对象的属性。如果此值为 null 或未提供,则对象的所有属性都包含在生成的 JSON 字符串中。

The replacer parameter can be either a function or an array.

replacer 参数可以是函数或数组。

As a function, it takes two parameters: the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter.

作为一个函数,它需要两个参数:键和被字符串化的值。找到键的对象作为替换器的 this 参数提供。

Initially, the replacer function is called with an empty string as key representing the object being stringified. It is then called for each property on the object or array being stringified.

最初,用一个空字符串作为表示被字符串化的对象的键来调用替换器函数。然后为正在字符串化的对象或数组上的每个属性调用它。