Alex
Alex

Reputation: 3790

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?

Upvotes: 4

Views: 3826

Answers (2)

hardus
hardus

Reputation: 41

i used this hack...

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

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

Upvotes: 2

Matt Ball
Matt Ball

Reputation: 359836

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"}'

Upvotes: 11

Related Questions