/** * Processing of submitted json data */ //form serialization, convert to a single json object $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { 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; }; //form serialization, convert to json list $.fn.serializeJson = function(){ var jsonData1 = {}; var serializeArray = this.serializeArray(); // 先转换成{"id": ["12","14"], "name": ["aaa","bbb"], "pwd":["pwd1","pwd2"]}这种形式 $(serializeArray).each(function () { if (jsonData1[this.name]) { if ($.isArray(jsonData1[this.name])) { jsonData1[this.name].push(this.value); } else { jsonData1[this.name] = [jsonData1[this.name], this.value]; } } else { jsonData1[this.name] = this.value; } }); // 再转成[{"id": "12", "name": "aaa", "pwd":"pwd1"},{"id": "14", "name": "bb", "pwd":"pwd2"}]的形式 var vCount = 0; // Calculate the maximum length of the array inside the json for(var item in jsonData1){ var tmp = $.isArray(jsonData1[item]) ? jsonData1[item].length : 1; vCount = (tmp > vCount) ? tmp : vCount; } if(vCount > 1) { var jsonData2 = new Array(); for(var i = 0; i < vCount; i++){ var jsonObj = {}; for(var item in jsonData1) { jsonObj[item] = jsonData1[item][i]; } jsonData2.push(jsonObj); } return JSON.stringify(jsonData2); }else{ return "[" + JSON.stringify(jsonData1) + "]"; } };
form is serialized into json object, json array
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325167561&siteId=291194637
Recommended
Ranking