Url To json - json To Url

jsonToUrl :

export const jsonToUrl = (data) => {
try {
var tempArr = [];
for (var i in data) {
if (encodeURIComponent(data[i]) != 'undefined' && encodeURIComponent(data[i]) != null && encodeURIComponent(data[i]) != '' && encodeURIComponent(data[i])) {
var key = encodeURIComponent(i);
var value = encodeURIComponent(data[i]);
tempArr.push(key + '=' + value);
}
}
var urlParamsStr = tempArr.join('&');
return urlParamsStr;
} catch (err) {
return '';
}
}

UrlTo json:

let query ='nickName=nickName&license=license&corporate=corporate&idCard=idCard&password=password';
let queryObj = query ? JSON.parse('{"' + query.replace(/&/g, '","').replace(/=/g, '":"') + '"}') : {};
console.log(queryObj)

 

posted @ 2020-08-10 09:21  mrt_yy  阅读(203)  评论(0编辑  收藏  举报