我正在将表单数据转换为 json 数据。这是我的代码:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function() {
$('form').submit(function() {
$('#result').append(JSON.stringify($('form').serializeObject()));
return false;
});
});
上面的代码给出了以下 json 字符串:
{"TypeName":["type1","type2"],"rate":["23","24"],"no:ofTickets":["23","24"],"allowedMembers": ["member1","member2"]}
但我需要得到这样的json:
{[
{"TypeName":"type1","rate":"23","no:ofTickets":"23","allowedMembers":"member1"},
{"TypeName":"type2","rate":"34","no:ofTickets":"56","allowedMembers":"member2"}
]}
我该如何解决我的问题??
谢谢