如何解码用 x-www-form-urlencoded 编码的 Javascript 对象数组?

How to decode array of Javascript object encoded with x-www-form-urlencoded?

我用 POST 请求发送以下数组:

let bookings = [{
  owner       : clientId,
  businessId  : businessId,
  service     : serviceId,
  startDate   : new Date()
}, {
  owner       : clientId,
  businessId  : businessId,
  service     : serviceId,
  startDate   : new Date()
}];

在服务器上我有以下设置:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

并得到以下req.body内容:

0[owner]=56fd4dcf8b98ce1c3d87e3ad, 0[businessId]=56fd4dcf8b98ce1c3d87e3ac, 0[service]=56fd4dcf8b98ce1c3d87e3ab, 0[startDate]=2016-03-31T16:18:23.220Z, 1[owner]=56fd4dcf8b98ce1c3d87e3ad, 1[businessId]=56fd4dcf8b98ce1c3d87e3ac, 1[service]=56fd4dcf8b98ce1c3d87e3ab, 1[startDate]=2016-03-31T16:18:23.220Z

如何将其解码回 JavaScript 对象数组?

通过body-parser使用urlencoded()函数。

app.use(bodyParser.urlencoded({extended: true}));

extended

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The "extended" syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please see the qs library.

Defaults to true, but using the default has been deprecated. Please research into the difference between qs and querystring and choose the appropriate setting.