Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I convert a querystring into a json object in jquery

This seems like a no brainer but surely there is either an internal js method or a jquery one to take a string like:

intTime=1324443870&fltOriginalAmount=0.00&strOriginalCurrency=GBP

...then a lot more vals and turn it into a JSON object?

I had a dig around this site and google and surprisingly drew blanks... Anyone got an easy way to do this?

like image 858
Alex Avatar asked Dec 12 '22 06:12

Alex


2 Answers

jQuery BBQ does exactly this. See $.deparam, "The opposite of jQuery.param, pretty much."

> var obj = $.deparam('intTime=1324443870&fltOriginalAmount=0.00&strOriginalCurrency=GBP')
> JSON.stringify(obj)
  '{"intTime":"1324443870","fltOriginalAmount":"0.00","strOriginalCurrency":"GBP"}'
like image 184
Matt Ball Avatar answered Dec 21 '22 23:12

Matt Ball


i used this hack...

$.parseJSON('{"' + qs.replace(/&/g, '","').replace(/=/g, '":"') + '"}');

demo here http://jsbin.com/niqaw/

like image 27
hardus Avatar answered Dec 21 '22 23:12

hardus