Forked from brucekirkpatrick/jquery.unserialize.js
Last active
December 21, 2018 09:39
-
-
Save lyoshenka/14a41873cc50578ae5f2 to your computer and use it in GitHub Desktop.
Takes a string in format "param1=value1¶m2=value2" and returns an javascript object. The opposite of https://api.jquery.com/serialize
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 } }) | |
* Usage example: console.log(unserialize("one="+escape("& = ?")+"&two="+escape("value1")+"&two="+escape("value2")+"&three[]="+escape("value1")+"&three[]="+escape("value2"))); | |
*/ | |
unserialize = function(serializedString) { | |
var str = decodeURI(serializedString), | |
pairs = str.split('&'), | |
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this jQuery function do exactly the same as this js function?
https://github.com/kvz/phpjs/blob/master/functions/var/unserialize.js