乐趣区

关于前端:JavaScript把url-get参数转为JSON对象

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 格式化工具

退出移动版