npm install qs
命令进行安装. import qs from 'qs'
qs.parse(location.search.replace(/^\?/, ''))
const url = "http://localhost:8000/abcd?aa=111&bb=222"
const qsObject = qs.parse(location.search.replace(/^\?/, ''))
// location.search.replace(/^\?/, '') = "aa=111&bb=222"
// qsObject = {aa:11,bb:22}
qs.parse()、qs.stringify()、JSON.parse()、JSON.stringify()使用方法
一、JSON.parse(用于从一个字符串中解析出json 对象)ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常
let 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"}]';
JSON.parse(str);//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"}
]
二、JSON.stringify(用于从一个对象解析出字符串)
let 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"}
]
JSON.stringify(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"}]"
三、qs.parse()将URL解析成对象的形式
let url = 'method=query_sql_dataset_data&projectId=85&appToken=7d22e38e-5717-11e7-907b-a6006ad3dba0';
qs.parse(url);
//{method: "query_sql_dataset_data", projectId: "85", appToken: "7d22e38e-5717-11e7-907b-a6006ad3dba0"}
四、qs.stringify( )将对象解析成URL的形式
let obj= {method: "query_sql_dataset_data", projectId: "85", appToken: "7d22e38e-5717-11e7-907b-a6006ad3dba0", datasetId: " 12564701"};
qs.stringify( obj)
//{method: "query_sql_dataset_data", projectId: "85", appToken: "7d22e38e-5717-11e7-907b-a6006ad3dba0", datasetId: " 12564701"}