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)