引入 queryString 模块

1
const querystring = require("querystring");

序列化 queryString

querystring.stringify(obj, para1, para2);

  • objquery的对象
  • para1是参数之间的连接符。默认为”&”
  • para2keyvalue之间的连接符。默认为”=”
1
2
3
queryString.stringify({ name: "scott", course: ["jade", "node"], from: "" });
//result
//"name=scott&course=jade&course=node&from=";

第二个参数

1
2
3
4
5
6
queryString.stringify(
{ name: "scott", course: ["jade", "node"], from: "" },
","
);
// result
//'name=scott,course=jade,course=node,from='

第三个参数

1
2
3
4
5
6
7
queryString.stringify(
{ name: "scott", course: ["jade", "node"], from: "" },
",",
":"
);
// result
//'name:scott,course:jade,course:node,from:'

反序列化

querystring.parse(string,para1,para2);

  • stringurlquery的字符串
  • para1是连接符
  • para2keyvalue之间的连接符
1
2
3
4
5
6
7
querystring.parse("name=scott,course=jade,course=node,from=", ",");
// result
/*{
name: 'scott',
course: ['jade','node'],
form: ""
}*/