For this example, assume that I have a list of months in a form, each with a checkbox next to them. I'm looking for help on doing either of two things:
- Convert a query string (e.g.
"January=on&March=on&September=on"
) or - Convert an object map:
[{ January: 'on' },{ March: 'on' },{ September: 'on' }]
to a single JSON object: { January: 'on', March: 'on', September: 'on' }
I realize that the first map is already JSON, but instead of the object array I'd like it to be a single JSON object. I can build the map with $('form').serializeArray();
and I can build the query string with $('form').serialize();
.
The implementation of .serialize() in the jQuery API is simply:
serialize: function() {
return jQuery.param(this.serializeArray());
},
which is why I could handle either the first or second answer.
The reason I'd like to do this is because I'm switching from PrototypeJS to jQuery, and in PrototypeJS this was as simple as:
Object.toJSON(Form.serializeElements($('myform'), true));
So, does anybody know of a JSON plug-in (I'd like to stick with ONLY jQuery) that could do this easily, or know of a simple method to achieve the result I'm looking for? Thanks!