LoginSignup
3
3

More than 3 years have passed since last update.

【Node.js】オブジェクト(連想配列)とクエリ文字列の相互変換

Posted at

サンプルコード

querystringモジュールを使用する。
Node.jsの標準モジュールのため、事前準備等は不要。

const querystring = require('querystring');

// オブジェクト -> クエリ文字列
const obj     = { a: '1', b: '2' };
const result1 = querystring.stringify(obj);
console.log(result1);
// => a=1&b=2

// クエリ文字列 -> オブジェクト
const str     = 'a=1&b=2';
const result2 = querystring.parse(str);
console.log(result2);
// => [Object: null prototype] { a: '1', b: '2' }

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3