querystring.parse()方法用於將URL查詢字符串解析為包含查詢URL的鍵和對值的對象。返回的對象不會從JavaScript對象繼承原型,因此常規的Object方法將不起作用。在解析過程中,除非有其他字符編碼格式,否則均采用UTF-8編碼格式。要解碼其他字符編碼,必須指定encodeURIComponent選項。
用法:
querystring.parse( str[, sep[, eq[, options]]]) )
參數:該函數接受上述和以下所述的四個參數:
- str:它是一個字符串,它指定必須解析的URL查詢。
 - sep:它是一個字符串,它指定用於分隔指定查詢字符串中的鍵和值對的子字符串。默認值為“&”。
 - eq:它是一個字符串,它指定用於分隔指定查詢字符串中的鍵和值的子字符串。默認值為“=”。
 - options:它是可用於修改方法行為的對象。它具有以下參數:
- decodeURIComponent:此函數將用於解碼查詢字符串中的percent-encoded個字符。默認值為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);輸出:
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:
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] {
  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
相關用法
- Node.js console.timeLog()用法及代碼示例
 - Node.js GM drawLine()用法及代碼示例
 - Node.js GM border()用法及代碼示例
 - Node.js GM median()用法及代碼示例
 - Node.js GM drawRectangle()用法及代碼示例
 - Node.js GM negative()用法及代碼示例
 - Node.js GM threshold()用法及代碼示例
 - Node.js GM contrast()用法及代碼示例
 - Node.js GM lower()用法及代碼示例
 - Node.js GM scale()用法及代碼示例
 - Node.js GM bordercolor()用法及代碼示例
 - Node.js GM flip()用法及代碼示例
 - Node.js GM flop()用法及代碼示例
 - Node.js GM roll()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | querystring.parse() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
