如何在jquery中将查询字符串转换为json对象

时间:2022-10-30 14:17:00

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

这似乎没什么好事但是肯定有一个内部的js方法或一个jquery来获取一个字符串:

intTime=1324443870&fltOriginalAmount=0.00&strOriginalCurrency=GBP

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

...然后更多的val并将其转换为JSON对象?

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

我在这个网站和谷歌进行了挖掘,并且令人惊讶地画了空白......任何人都有一个简单的方法来做到这一点?

2 个解决方案

#1


11  

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

jQuery BBQ正是这样做的。请参阅$ .deparam,“与jQuery.param完全相反。”

> var obj = $.deparam('intTime=1324443870&fltOriginalAmount=0.00&strOriginalCurrency=GBP')
> JSON.stringify(obj)
  '{"intTime":"1324443870","fltOriginalAmount":"0.00","strOriginalCurrency":"GBP"}'

#2


2  

i used this hack...

我用过这个黑客......

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

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

在这里演示http://jsbin.com/niqaw/

#1


11  

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

jQuery BBQ正是这样做的。请参阅$ .deparam,“与jQuery.param完全相反。”

> var obj = $.deparam('intTime=1324443870&fltOriginalAmount=0.00&strOriginalCurrency=GBP')
> JSON.stringify(obj)
  '{"intTime":"1324443870","fltOriginalAmount":"0.00","strOriginalCurrency":"GBP"}'

#2


2  

i used this hack...

我用过这个黑客......

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

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

在这里演示http://jsbin.com/niqaw/