將url中的參數字符串轉換成json對象。sql
var qs = require('qs'); var url = 'method=query_sql_dataset_data&projectId=85&appToken=7d22e38e-5717-11e7-907b-a6006ad3dba0'; console.log(qs.parse(url));
控制檯輸出爲:json
{ method: 'query_sql_dataset_data', projectId: '85', appToken: '7d22e38e-5717-11e7-907b-a6006ad3dba0' }
將json對象轉換成url中的參數字符串。app
var qs = require('qs'); let obj = { method: "query_sql_dataset_data", projectId: "85", appToken: "7d22e38e-5717-11e7-907b-a6006ad3dba0", datasetId: " 12564701" }; console.log(qs.stringify(obj))
控制檯輸出爲:大數據
method=query_sql_dataset_data&projectId=85&appToken=7d22e38e-5717-11e7-907b-a6006ad3dba0&datasetId=%2012564701
將一個字符串中解析成json對象,ps:對於字符串,單引號寫在{}
外,每一個屬性都必須雙引號,不然會拋出異常ui
var str = '[{"field":"thedate","v1":"20170102"},{"field":"rev_type","v1":"大數據收入"},{"field":"thismonth","v1":"201708"},{"field":"nextmonth","v1":"201709"},{"field":"depart_type","v1":"leader"},{"field":"admin_emp_id","v1":"role"}]'; console.log(JSON.parse(str));
控制檯輸出爲:this
[ { field: 'thedate', v1: '20170102' }, { field: 'rev_type', v1: '大數據收入' }, { field: 'thismonth', v1: '201708' }, { field: 'nextmonth', v1: '201709' }, { field: 'depart_type', v1: 'leader' }, { field: 'admin_emp_id', v1: 'role' } ]
將一個json對象轉換成字符串,生成的字符串全都是雙引號。url
var arr=[ {field: "thedate", v1: "20170102"}, {field: "rev_type", v1: "大數據收入"}, {field: "thismonth", v1: "201708"}, {field: "nextmonth", v1: "201709"}, {field: "depart_type", v1: "leader"}, {field: "admin_emp_id", v1: "role"} ] console.log(JSON.stringify(arr))
控制檯輸出爲:3d
[{"field":"thedate","v1":"20170102"},{"field":"rev_type","v1":"大數據收入"},{"field":"thismonth","v1":"201708"},{"field":"nextmonth","v1":"201709"},{"field":"depart_type","v1":"leader"},{"field":"admin_emp_id","v1":"role"}]
qs是用於json對象與查詢字符串之間進行轉換。code
JSON是用於json對象與json格式的字符串之間進行轉換。對象
parse()方法的返回值都是json對象。