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

标签 javascript json function stringify

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

JSON.stringify(obj, null, 4)

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

谢谢。

最佳答案

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

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

来自 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

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.

关于javascript - JavaScript 中 JSON.stringify 中的第二个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17537571/

相关文章:

javascript - 如何在innerHTML中使用onClick函数

javascript - 我怎样才能阻止这个事件发生?

php - 使用 JSON 和 PHP 获取维基百科内容摘录

c++ - 模板成员函数无法自动推断类型

function - 在 GHCi 中,为什么函数箭头 `:kind (->)` 的类型包含问号 `(->)::?? -> ? -> *` ?

javascript - 在 iPad 上绝对定位不会将元素设置在所需位置

javascript - 在移动设备的同一页面中打开 Stripe 结帐弹出窗口

javascript - PHP 和 JSON 获取关注者数量

ios - AFJSONResponseSerializer 后 JSON NSDictionary 困惑

c - 从函数更新指向 c 数组的指针