📜  Node.js querystring.parse() 方法

📅  最后修改于: 2022-05-13 01:56:46.631000             🧑  作者: Mango

Node.js querystring.parse() 方法

querystring.parse() 方法用于将 URL 查询字符串解析为包含查询 URL 的键和对值的对象。返回的对象不会从 JavaScript 对象继承原型,因此通常的 Object 方法将不起作用。在解析期间,除非有替代的字符编码格式,否则假定使用 UTF-8 编码格式。要解码替代字符编码,必须指定 decodeURIComponent 选项。

句法:

querystring.parse( str[, sep[, eq[, options]]]) )

参数:该函数接受上面提到的四个参数,如下所述:

  • str:它是一个字符串,指定必须解析的 URL 查询。
  • sep:它是一个String,它指定用于分隔指定查询字符串中的键值对的子字符串。默认值为“&”。
  • eq:它是一个String,它指定用于分隔指定查询字符串中的键和值的子字符串。默认值为“=”。
  • options:它是一个可用于修改方法行为的对象。它具有以下参数:
    • decodeURIComponent:它是一个函数,用于解码查询字符串中的百分比编码字符。默认值为 querystring.unescape()。
    • maxKeys:它是一个数字,它指定应该从查询字符串中解析的最大键数。 “0”值将删除所有计数限制。默认值为“1000”。

返回值:它返回一个对象,该对象具有从查询字符串中解析的键值对。

下面的示例说明了 Node.js 中的querystring.parse()方法:

示例 1:

Node.js
// Import the querystring module
const querystring = require("querystring");
  
// Specify the URL query string
// to be parsed
let urlQuery = 
  "username=user1&units=kgs&units=pounds&permission=false";
  
// Use the parse() method on the string
let parsedObject = querystring.parse(urlQuery);
  
console.log("Parsed Query:", parsedObject);
  
// Use the parse() method on the string
// with sep as `&&` and eq as `-`
urlQuery = 
  "username-user1&&units-kgs&&units-pounds&&permission-false";
parsedObject = querystring.parse(urlQuery, "&&", "-");
  
console.log("\nParsed Query:", parsedObject);


Node.js
// Import the querystring module
const querystring = require("querystring");
  
// Specify the URL query string
// to be parsed
let urlQuery = 
  "user=admin&articles=1&articles=2&articles=3&access=true";
  
// Use the parse() method on the string
// with default values
let parsedObject = querystring.parse(urlQuery, "&", "=");
  
console.log("Parsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 1
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 1 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 2
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 2 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 0 (no limits)
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 0 });
  
console.log("\nParsed Query:", parsedObject);


输出:

Parsed Query: [Object: null prototype] {
  username: 'user1',
  units: [ 'kgs', 'pounds' ],
  permission: 'false'
}

Parsed Query: [Object: null prototype] {
  username: 'user1',
  units: [ 'kgs', 'pounds' ],
  permission: 'false'
}

示例 2:

节点.js

// Import the querystring module
const querystring = require("querystring");
  
// Specify the URL query string
// to be parsed
let urlQuery = 
  "user=admin&articles=1&articles=2&articles=3&access=true";
  
// Use the parse() method on the string
// with default values
let parsedObject = querystring.parse(urlQuery, "&", "=");
  
console.log("Parsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 1
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 1 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 2
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 2 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 0 (no limits)
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 0 });
  
console.log("\nParsed Query:", parsedObject);

输出:

Parsed Query: [Object: null prototype] {
  user: 'admin',
  articles: [ '1', '2', '3' ],
  access: 'true'
}

Parsed Query: [Object: null prototype] { user: 'admin' }

Parsed Query: [Object: null prototype] 
              { user: 'admin', articles: '1' }

Parsed Query: [Object: null prototype] {
  user: 'admin',
  articles: [ '1', '2', '3' ],
  access: 'true'
}

参考: https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options