JavaScript把url get参数转为JSON对象。
Mar 21, 2020 10:34:15 PM
JavaScript把url get参数转为JSON对象
get 参数就是采用在请求的url
后面跟上?然后再以key=value
的方式传参,多组采用 “&”间隔。下面直接上方法。
//传入一个url即可
function getData(url) {
var elemt;
var data = {};
var sburl = url.slice(url.indexOf('?') + 1).split('&');
for (var i = 0; i < sburl.length; i++) {
elemt = sburl[i].split('=');
data[elemt[0]] = elemt[1];
}
return data;
}
GET参数转JSON测试
随便找一条URL
http://huodong.m.tmall.com/act/talent/live.html?id=256805675748&type=508&livesource=share&sourceType=talent&suid=69deae8e-9297-4748-ac4a-1f56a9c3d2db&ut_sk=1.Xa0uWOeDqLADAA8WuiK7Tieb_21646297_1584756300504.Copy.zhibo&un=7848d2289319471d14ac4609adad532d&share_crt_v=1&spm=a2159r.13376460.0.0&sp_tk=77+lMVhZdTE3UnNvSmTvv6U=
结果输出:
{
"id": "256805675748",
"type": "508",
"livesource": "share",
"sourceType": "talent",
"suid": "69deae8e-9297-4748-ac4a-1f56a9c3d2db",
"ut_sk": "1.Xa0uWOeDqLADAA8WuiK7Tieb_21646297_1584756300504.Copy.zhibo",
"un": "7848d2289319471d14ac4609adad532d",
"share_crt_v": "1",
"spm": "a2159r.13376460.0.0",
"sp_tk": "77+lMVhZdTE3UnNvSmTvv6U"
}
备注:JSON格式化工具
版权所属:JavaScript加密
原文地址:https://www.jsjiami.com/article/get-to-json.html
转载时必须以链接形式注明原始出处及本声明。