querystring.stringify(obj[, sep[, eq[, options]]])
obj<Object> 要序列化为 URL 查询字符串的对象sep<string> 用于分隔查询字符串中键值对的子字符串。默认值:'&'。eq<string>。用于在查询字符串中分隔键和值的子字符串。默认值:'='。optionsencodeURIComponent<Function> 在将 URL 不安全字符转换为查询字符串中的百分号编码时使用的函数。默认值:querystring.escape()。
querystring.stringify() 方法通过遍历对象的“自有属性”,从给定的 obj 生成 URL 查询字符串。
【The querystring.stringify() method produces a URL query string from a
given obj by iterating through the object's "own properties".】
它会序列化传入 obj 的以下类型的值:
<string> | <number> | <bigint> | <boolean> | <string[]> | <number[]> | <bigint[]> | <boolean[]>
数值必须是有限的。任何其他输入值都会被强制转换为空字符串。
【It serializes the following types of values passed in obj:
<string> | <number> | <bigint> | <boolean> | <string[]> | <number[]> | <bigint[]> | <boolean[]>
The numeric values must be finite. Any other input values will be coerced to
empty strings.】
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
// Returns 'foo=bar&baz=qux&baz=quux&corge='
querystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
// Returns 'foo:bar;baz:qux' 默认情况下,查询字符串中需要进行百分比编码的字符将会以 UTF-8 编码。如果需要使用其他编码,则需要指定一个替代的 encodeURIComponent 选项:
【By default, characters requiring percent-encoding within the query string will
be encoded as UTF-8. If an alternative encoding is required, then an alternative
encodeURIComponent option will need to be specified:】
// Assuming gbkEncodeURIComponent function already exists,
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
{ encodeURIComponent: gbkEncodeURIComponent });