网络日志

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