Convert URL to json

前端 未结 6 1271
太阳男子
太阳男子 2020-12-09 04:55

I can\'t seem to find an answer to this question.. How can I convert a URL parameters string to JSON in javascript? I mean to ask if there is an in-built function like this

6条回答
  •  佛祖请我去吃肉
    2020-12-09 05:20

    Try this :

    var str = 'some1=param&some2=param2';
    
    JSON.parse('{"' + decodeURI(str).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"').replace(/\s/g,'') + '"}')
    
    // {some1: "param1", some2: "param2"}
    

提交回复
热议问题