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/[email protected]/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.