-
-
Save JonatanFlores/33e7510c542227dd7f5f737de8eee938 to your computer and use it in GitHub Desktop.
$.unserialize for jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $.unserialize | |
* | |
* Takes a string in format "param1=value1¶m2=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¶m2=value2 | |
* Return: { param1 : value1, param2: value2 } | |
* | |
* Input: param1[]=value1¶m1[]=value2 | |
* Return: { param1: [ value1, value2 ] } | |
* | |
* @todo Support params like "param1[name]=value1" (should return { param1: { name: value1 } }) | |
*/ | |
(function($){ | |
$.unserialize = function(serializedString){ | |
var str = decodeURI(serializedString); | |
var pairs = str.split('&'); | |
var obj = {}, p, idx, val; | |
for (var i=0, n=pairs.length; i < n; i++) { | |
p = pairs[i].split('='); | |
idx = p[0]; | |
if (idx.indexOf("[]") == (idx.length - 2)) { | |
// Eh um vetor | |
var ind = idx.substring(0, idx.length-2) | |
if (obj[ind] === undefined) { | |
obj[ind] = []; | |
} | |
obj[ind].push(p[1]); | |
} | |
else { | |
obj[idx] = p[1]; | |
} | |
} | |
return obj; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment