We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

chrisaiv on 03/28/08


Tagged

json


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

iTony
ischenkodv


Params to JSON


Published in: JavaScript 


URL: http://www.mail-archive.com/discuss@jquery.com/msg20107.html

I found this neat little utility that will convert an array into JSON


  1. function params2json(d) {
  2. if (d.constructor != Array) {
  3. return d;
  4. }
  5. var data={};
  6. for(var i=0;i<d.length;i++) {
  7. if (typeof data[d[i].name] != 'undefined') {
  8. if (data[d[i].name].constructor!= Array) {
  9. data[d[i].name]=[data[d[i].name],d[i].value];
  10. } else {
  11. data[d[i].name].push(d[i].value);
  12. }
  13. } else {
  14. data[d[i].name]=d[i].value;
  15. }
  16. }
  17. return data;
  18. };

Report this snippet 

You need to login to post a comment.