Skip to content

Instantly share code, notes, and snippets.

@nmeylan
Last active August 29, 2015 14:00
Show Gist options
  • Save nmeylan/11502628 to your computer and use it in GitHub Desktop.
Save nmeylan/11502628 to your computer and use it in GitHub Desktop.
jQuery serialize form in JSON
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
//JSON
$.fn.serializeJSON = function () {
var json = {};
jQuery.map($(this).serializeArray(), function (n, i) {
if(n['name'].endsWith('[]')){
if(json[n['name']] === undefined)
json[n['name']] = [];
json[n['name']].push(n['value']);
}
else
json[n['name']] = n['value'];
});
return json;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment