国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

npm qs模塊使用詳解

2024-05-06 15:44:15
字體:
供稿:網(wǎng)友

本文基本使用谷歌翻譯加上自己的理解,權(quán)當(dāng)加深記憶。

npm

簡介

qs 是一個(gè)增加了一些安全性的查詢字符串解析和序列化字符串的庫。
主要維護(hù)者:Jordan Harband
最初創(chuàng)建者和維護(hù)者:TJ Holowaychuk

用法

var qs = require('qs');var assert = require('assert'); var obj = qs.parse('a=c');assert.deepEqual(obj, { a: 'c' }); var str = qs.stringify(obj);assert.equal(str, 'a=c');

解析對象

qs.parse(string, [options]);

qs 允許在查詢字符串中使用[]的方式創(chuàng)建嵌套的對象。例如,字符串'foo[bar]=baz'可以轉(zhuǎn)換為:

  assert.deepEqual(qs.parse('foo[bar]=baz'), {    foo: {      bar: 'baz'    }  });

When using the plainObjects option the parsed value is returned as a null object, created via Object.create(null) and as such you should be aware that prototype methods will not exist on it and a user may set those names to whatever value they like:

  var nullObject = qs.parse('a[hasOwnProperty]=b', { plainObjects: true });  assert.deepEqual(nullObject, { a: { hasOwnProperty: 'b' } });

By default parameters that would overwrite properties on the object prototype are ignored, if you wish to keep the data from those fields either use plainObjects as mentioned above, or set allowPrototypes to true which will allow user input to overwrite those properties. WARNING It is generally a bad idea to enable this option as it can cause problems when attempting to use the properties that have been overwritten. Always be careful with this option.

  var protoObject = qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true });  assert.deepEqual(protoObject, { a: { hasOwnProperty: 'b' } });

也可以解析 URI 編碼:

  assert.deepEqual(qs.parse('a%5Bb%5D=c'), {    a: { b: 'c' }  });

還可以像這樣嵌套對象:'foo[bar][baz]=foobarbaz':

  assert.deepEqual(qs.parse('foo[bar][baz]=foobarbaz'), {    foo: {      bar: {        baz: 'foobarbaz'      }    }  });

當(dāng)使用嵌套對象時(shí),qs 在默認(rèn)情況下最多解析到的深度是第五層(注:從第一個(gè)方括號到算起,到第五個(gè)方括號),例如嘗試解析一個(gè)這樣的字符串'a[b][c][d][e][f][g][h][i]=j'將會得到以下結(jié)果:

  var expected = {    a: {      b: {        c: {          d: {            e: {              f: {                '[g][h][i]': 'j'              }            }          }        }      }    }  };  var string = 'a[b][c][d][e][f][g][h][i]=j';  assert.deepEqual(qs.parse(string), expected);

可以給 qs.parse 傳遞一個(gè) depth 參數(shù)覆蓋默認(rèn)值:

var deep = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });assert.deepEqual(deep, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } });             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 房产| 漳浦县| 苏尼特左旗| 高邮市| 大荔县| 建湖县| 英德市| 额尔古纳市| 四川省| 阿鲁科尔沁旗| 三江| 庆安县| 修水县| 铜川市| 东山县| 大宁县| 沁阳市| 辽宁省| 和平区| 津南区| 木里| 峨眉山市| 宁武县| 灵山县| 凤台县| 黄石市| 青海省| 泰安市| 二连浩特市| 新兴县| 大关县| 顺义区| 温宿县| 蒙山县| 孟津县| 新民市| 绥滨县| 张家口市| 从江县| 应城市| 石泉县|