Skip to content

Instantly share code, notes, and snippets.

@brucekirkpatrick
Forked from rcmachado/jquery.unserialize.js
Last active March 11, 2018 22:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save brucekirkpatrick/7026682 to your computer and use it in GitHub Desktop.
Save brucekirkpatrick/7026682 to your computer and use it in GitHub Desktop.
Takes a string in format "param1=value1&param2=value2" and returns an javascript object. Improved to handle multiple values with same name and to unescape url encoded values.
/**
* $.unserialize
*
* Takes a string in format "param1=value1&param2=value2" and returns an object { param1: 'value1', param2: 'value2' }. If the "param1" ends with "[]" the param is treated as an array.
*
* Example:
*
* Input: param1=value1&param2=value2
* Return: { param1 : value1, param2: value2 }
*
* Input: param1[]=value1&param1[]=value2
* Return: { param1: [ value1, value2 ] }
*
* @todo Support params like "param1[name]=value1" (should return { param1: { name: value1 } })
* Usage example: console.log($.unserialize("one="+escape("& = ?")+"&two="+escape("value1")+"&two="+escape("value2")+"&three[]="+escape("value1")+"&three[]="+escape("value2")));
*/
(function($){
$.unserialize = function(serializedString){
var str = decodeURI(serializedString);
var pairs = str.split('&');
var obj = {}, p, idx;
for (var i=0, n=pairs.length; i < n; i++) {
p = pairs[i].split('=');
idx = p[0];
if (obj[idx] === undefined) {
obj[idx] = unescape(p[1]);
}else{
if (typeof obj[idx] == "string") {
obj[idx]=[obj[idx]];
}
obj[idx].push(unescape(p[1]));
}
}
return obj;
};
})(jQuery);
@mppatterson
Copy link

Awesome, thanks! 👍

@sahanDissanayake
Copy link

@brucekirkpatrick, There is a problem with your function
This is my link right

http://localhost:1337/?categories=Bakery&categories=Bar&categories=Beer+Garden&categories=Brazilian+Restaurant&cooking=&sorted=Magic

If you see close of there are two words the a + is added, need to fix this

@sahanDissanayake
Copy link

// https://gist.github.com/brucekirkpatrick/7026682
/**
 * $.unserialize
 *
 * Takes a string in format "param1=value1&param2=value2" and returns an object { param1: 'value1', param2: 'value2' }. If the "param1" ends with "[]" the param is treated as an array.
 *
 * Example:
 *
 * Input:  param1=value1&param2=value2
 * Return: { param1 : value1, param2: value2 }
 *
 * Input:  param1[]=value1&param1[]=value2
 * Return: { param1: [ value1, value2 ] }
 *
 * @todo Support params like "param1[name]=value1" (should return { param1: { name: value1 } })
 * Usage example: console.log($.unserialize("one="+escape("& = ?")+"&two="+escape("value1")+"&two="+escape("value2")+"&three[]="+escape("value1")+"&three[]="+escape("value2")));
 */
(function($){
    $.unserialize = function(serializedString){
        var str = decodeURI(serializedString);
        var pairs = str.split('&');
        var obj = {}, p, idx;
        for (var i=0, n=pairs.length; i < n; i++) {
            p = pairs[i].split('=');
            idx = p[0]; 
            if (obj[idx] === undefined) {
                obj[idx] = unescape(p[1]).replace ( /\+/g, ' ' );
            }else{
                if (typeof obj[idx] == "string") {
                    obj[idx]=[obj[idx]];
                }
                obj[idx].push(unescape(p[1]).replace ( /\+/g, ' ' ));
            }
        }
        return obj;
    };
})(jQuery);

@Pharaoh2k
Copy link

Does this jQuery function do exactly the same as this js function?
https://github.com/kvz/phpjs/blob/master/functions/var/unserialize.js

@DannyFeliz
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment