How to convert URL parameters to a JavaScript object?

前端 未结 30 1031
时光取名叫无心
时光取名叫无心 2020-11-22 13:57

I have a string like this:

abc=foo&def=%5Basf%5D&xyz=5

How can I convert it into a JavaScript object like this?

{
          


        
相关标签:
30条回答
  • 2020-11-22 14:41

    Edit

    This edit improves and explains the answer based on the comments.

    var search = location.search.substring(1);
    JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')
    

    Example

    Parse abc=foo&def=%5Basf%5D&xyz=5 in five steps:

    • decodeURI: abc=foo&def=[asf]&xyz=5
    • Escape quotes: same, as there are no quotes
    • Replace &: abc=foo","def=[asf]","xyz=5
    • Replace =: abc":"foo","def":"[asf]","xyz":"5
    • Suround with curlies and quotes: {"abc":"foo","def":"[asf]","xyz":"5"}

    which is legal JSON.

    An improved solution allows for more characters in the search string. It uses a reviver function for URI decoding:

    var search = location.search.substring(1);
    JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) })
    

    Example

    search = "abc=foo&def=%5Basf%5D&xyz=5&foo=b%3Dar";
    

    gives

    Object {abc: "foo", def: "[asf]", xyz: "5", foo: "b=ar"}
    

    Original answer

    A one-liner:

    JSON.parse('{"' + decodeURI("abc=foo&def=%5Basf%5D&xyz=5".replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}')
    
    0 讨论(0)
  • 2020-11-22 14:43

    This seems to be the best solution as it takes multiple parameters of the same name into consideration.

        function paramsToJSON(str) {
            var pairs = str.split('&');
            var result = {};
            pairs.forEach(function(pair) {
                pair = pair.split('=');
                var name = pair[0]
                var value = pair[1]
                if( name.length )
                    if (result[name] !== undefined) {
                        if (!result[name].push) {
                            result[name] = [result[name]];
                        }
                        result[name].push(value || '');
                    } else {
                        result[name] = value || '';
                    }
            });
            return( result );
        }
    
    <a href="index.html?x=1&x=2&x=3&y=blah">something</a>
    paramsToJSON("x=1&x=2&x=3&y=blah"); 
    
    console yields => {x: Array[3], y: "blah"} where x is an array as is proper JSON
    

    I later decided to convert it to a jQuery plugin too...

    $.fn.serializeURLParams = function() {
        var result = {};
    
        if( !this.is("a") || this.attr("href").indexOf("?") == -1 ) 
            return( result );
    
        var pairs = this.attr("href").split("?")[1].split('&');
        pairs.forEach(function(pair) {
            pair = pair.split('=');
            var name = decodeURI(pair[0])
            var value = decodeURI(pair[1])
            if( name.length )
                if (result[name] !== undefined) {
                    if (!result[name].push) {
                        result[name] = [result[name]];
                    }
                    result[name].push(value || '');
                } else {
                    result[name] = value || '';
                }
        });
        return( result )
    }
    
    <a href="index.html?x=1&x=2&x=3&y=blah">something</a>
    $("a").serializeURLParams(); 
    
    console yields => {x: Array[3], y: "blah"} where x is an array as is proper JSON
    

    Now, the first will accept the parameters only but the jQuery plugin will take the whole url and return the serialized parameters.

    0 讨论(0)
  • 2020-11-22 14:44

    FIRST U NEED TO DEFINE WHAT'S A GET VAR:

    function getVar()
    {
        this.length = 0;
        this.keys = [];
        this.push = function(key, value)
        {
            if(key=="") key = this.length++;
            this[key] = value;
            this.keys.push(key);
            return this[key];
        }
    }
    

    Than just read:

    function urlElement()
    {
        var thisPrototype = window.location;
        for(var prototypeI in thisPrototype) this[prototypeI] = thisPrototype[prototypeI];
        this.Variables = new getVar();
        if(!this.search) return this;
        var variables = this.search.replace(/\?/g,'').split('&');
        for(var varI=0; varI<variables.length; varI++)
        {
            var nameval = variables[varI].split('=');
            var name = nameval[0].replace(/\]/g,'').split('[');
            var pVariable = this.Variables;
            for(var nameI=0;nameI<name.length;nameI++)
            {
                if(name.length-1==nameI) pVariable.push(name[nameI],nameval[1]);
                else var pVariable = (typeof pVariable[name[nameI]] != 'object')? pVariable.push(name[nameI],new getVar()) : pVariable[name[nameI]];
            }
        }
    }
    

    and use like:

    var mlocation = new urlElement();
    mlocation = mlocation.Variables;
    for(var key=0;key<mlocation.keys.length;key++)
    {
        console.log(key);
        console.log(mlocation[mlocation.keys[key]];
    }
    
    0 讨论(0)
  • 2020-11-22 14:45

    Pretty easy using the URLSearchParams JavaScript Web API,

    var paramsString = "q=forum&topic=api";
    
    //returns an iterator object
    var searchParams = new URLSearchParams(paramsString);
    
    //Usage
    for (let p of searchParams) {
      console.log(p);
    }
    
    //Get the query strings
    console.log(searchParams.toString());
    
    //You can also pass in objects
    
    var paramsObject = {q:"forum",topic:"api"}
    
    //returns an iterator object
    var searchParams = new URLSearchParams(paramsObject);
    
    //Usage
    for (let p of searchParams) {
      console.log(p);
    }
    
    //Get the query strings
    console.log(searchParams.toString());

    Useful Links

    • URLSearchParams - Web APIs | MDN
    • Easy URL Manipulation with URLSearchParams | Web | Google Developers

    NOTE: Not Supported in IE

    0 讨论(0)
  • 2020-11-22 14:45

    Building on top of Mike Causer's answer I've made this function which takes into consideration multiple params with the same key (foo=bar&foo=baz) and also comma-separated parameters (foo=bar,baz,bin). It also lets you search for a certain query key.

    function getQueryParams(queryKey) {
        var queryString = window.location.search;
        var query = {};
        var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split('=');
            var key = decodeURIComponent(pair[0]);
            var value = decodeURIComponent(pair[1] || '');
            // Se possui uma vírgula no valor, converter em um array
            value = (value.indexOf(',') === -1 ? value : value.split(','));
    
            // Se a key já existe, tratar ela como um array
            if (query[key]) {
                if (query[key].constructor === Array) {
                    // Array.concat() faz merge se o valor inserido for um array
                    query[key] = query[key].concat(value);
                } else {
                    // Se não for um array, criar um array contendo o valor anterior e o novo valor
                    query[key] = [query[key], value];
                }
            } else {
                query[key] = value;
            }
        }
    
        if (typeof queryKey === 'undefined') {
            return query;
        } else {
            return query[queryKey];
        }
    }
    

    Example input: foo.html?foo=bar&foo=baz&foo=bez,boz,buz&bar=1,2,3

    Example output

    {
        foo: ["bar","baz","bez","boz","buz"],
        bar: ["1","2","3"]
    }
    
    0 讨论(0)
  • 2020-11-22 14:46

    I had the same problem, tried the solutions here, but none of them really worked, since I had arrays in the URL parameters, like this:

    ?param[]=5&param[]=8&othr_param=abc&param[]=string
    

    So I ended up writing my own JS function, which makes an array out of the param in URI:

    /**
     * Creates an object from URL encoded data
     */
    var createObjFromURI = function() {
        var uri = decodeURI(location.search.substr(1));
        var chunks = uri.split('&');
        var params = Object();
    
        for (var i=0; i < chunks.length ; i++) {
            var chunk = chunks[i].split('=');
            if(chunk[0].search("\\[\\]") !== -1) {
                if( typeof params[chunk[0]] === 'undefined' ) {
                    params[chunk[0]] = [chunk[1]];
    
                } else {
                    params[chunk[0]].push(chunk[1]);
                }
    
    
            } else {
                params[chunk[0]] = chunk[1];
            }
        }
    
        return params;
    }
    
    0 讨论(0)
提交回复
热议问题