querystring.parse(str[, sep[, eq[, options]]])
str
<string> 要解析的网址查询字符串sep
<string> 用于在查询字符串中分隔键值对的子字符串。 默认值:'&'
。eq
<string>用于在查询字符串中分隔键和值的子字符串。 默认值:'='
。options
<Object>decodeURIComponent
<Function> 当对查询字符串中的百分比编码字符进行解码时使用的函数。 默认值:querystring.unescape()
。maxKeys
<number> 指定要解析的最大键数。 指定0
以删除键的计数限制。 默认值:1000
。
querystring.parse()
方法将网址查询字符串 (str
) 解析为键值对的集合。
例如,查询字符串 'foo=bar&abc=xyz&abc=123'
被解析为:
{
foo: 'bar',
abc: ['xyz', '123']
}
querystring.parse()
方法返回的对象通常不是从 JavaScript Object
继承的原型。
这意味着典型的 Object
方法,例如 obj.toString()
、obj.hasOwnProperty()
和其他方法没有定义并且不会起作用。
默认情况下,查询字符串中的百分比编码字符将被假定为使用 UTF-8 编码。
如果使用替代的字符编码,则需要指定替代的 decodeURIComponent
选项:
// 假设 gbkDecodeURIComponent 函数已存在...
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
{ decodeURIComponent: gbkDecodeURIComponent });
str
<string> The URL query string to parsesep
<string> The substring used to delimit key and value pairs in the query string. Default:'&'
.eq
<string>. The substring used to delimit keys and values in the query string. Default:'='
.options
<Object>decodeURIComponent
<Function> The function to use when decoding percent-encoded characters in the query string. Default:querystring.unescape()
.maxKeys
<number> Specifies the maximum number of keys to parse. Specify0
to remove key counting limitations. Default:1000
.
The querystring.parse()
method parses a URL query string (str
) into a
collection of key and value pairs.
For example, the query string 'foo=bar&abc=xyz&abc=123'
is parsed into:
{
foo: 'bar',
abc: ['xyz', '123']
}
The object returned by the querystring.parse()
method does not
prototypically inherit from the JavaScript Object
. This means that typical
Object
methods such as obj.toString()
, obj.hasOwnProperty()
, and others
are not defined and will not work.
By default, percent-encoded characters within the query string will be assumed
to use UTF-8 encoding. If an alternative character encoding is used, then an
alternative decodeURIComponent
option will need to be specified:
// Assuming gbkDecodeURIComponent function already exists...
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
{ decodeURIComponent: gbkDecodeURIComponent });