I have a string like this:
abc=foo&def=%5Basf%5D&xyz=5
(it comes from ajaxOptions.data
parameter from the ajaxComplete event)
How can I convert it into a javascript object like this:
obj = {
abc: 'foo',
def: '[asf]',
xyz: 5
}
?
I have a string like this:
(it comes from How can I convert it into a javascript object like this:
? |
|||||||||
|
EditThis edit improves and explains the answer based on the comments.
Example Parse
which is legal JSON. An improved solution allows for more characters in the search string. It uses a reviver function for URI decoding:
Example
gives
Original answerA one-liner:
|
|||||||||||||||||||||
|
I found $.String.deparam the most complete pre built solution (can do nested objects etc.). Check out the documentation. |
|||||||||
|
This is the simple version, obviously you'll want to add some error checking:
|
|||||||||||||||||||||
|
Split on
Another approach, using regular expressions:
This is adapted from John Resig's "Search and Don’t Replace". |
|||||||||
|
There is no native solution that I'm aware of. Dojo has a built-in unserialization method if you use that framework by chance. Otherwise you can implement it yourself rather simply:
edit: added decodeURIComponent() |
||||
|
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:
So I ended up writing my own JS function, which makes an array out of the param in URI:
|
|||||
|
FIRST U NEED TO DEFINE WHAT'S A GET VAR:
Than just read:
and use like:
|
|||||
|