将前台JS数组序列化后提交,后台反序列化对象1. //前台js
function inputAll(thiss) {
var _entryArr = [];
var $trArr = $("#FirstTable tr:gt(0)");
if ($trArr.length == 0) { return; }
for (var i = 0; i < $trArr.length; i++) {
var $thirdTd = $($trArr[i]).find("td:eq(2)");
var entryObjectID = $thirdTd.find(":hidden:eq(0)").val();
var entryData = $thirdTd.find(":text:eq(0)").val();
if ($.trim(entryData).length == 0) { alert('请输⼊第' + (i + 1) + '⾏要录⼊的数据!'); return; }
var _entry = {};typeof array
_entry.EntryObjectID = entryObjectID;
_entry.EntryData = entryData;
_entryArr.push(_entry);
}
var _data = {};
_data.Action = 'InputAllData';
_data.EntryData_Arr = Serialize(_entryArr);
_data.EntryTime = $("#txtFirstStartTime").val();
$.ajax({
type: "get",
cache: false,
url: "OtherData.aspx",
data: _data,
beforeSend: function (XMLHttpRequest) {
showLoading('正在录⼊,请稍候...');
},
success: function (data) {
try {
hideLoading();
if (Number(data) >= 1) {
alert("批量录⼊成功!");
} else {
alert("批量录⼊失败!");
}
} catch (ex) {
alert("批量录⼊失败!");
}
},
error: function () {
hideLoading();
}
});
}
2. //后台:
3. string EntryData = Request["EntryData_Arr"];
//[{EntryObjectID:"7",EntryData:"121.222"},{EntryObjectID:"8",EntryData:"115.3"},{EntryObjectID:"9",EntryData:"166.8"}, {EntryObjectID:"1",EntryData:"0"}]
tempEntryModel[] EntryArr = (tempEntryModel[])Newtonsoft.Json.JsonConvert.DeserializeObject(EntryData,
typeof(tempEntryModel[]));
2. //临时model类
3. private class tempEntryModel
{
//{EntryObjectID:"7",EntryData:"121.222"}
public int EntryObjectID { get; set; }
public double EntryData { get; set; }
}
4. //js序列化⽅法:
5. function Serialize(obj) {
switch (structor) {
case Object:
var str = "{";
for (var o in obj) {
str += o + ":" + Serialize(obj[o]) + ","; }
if (str.substr(str.length - 1) == ",")
str = str.substr(0, str.length - 1);
return str + "}";
break;
case Array:
var str = "[";
for (var o in obj) {
str += Serialize(obj[o]) + ",";
}
if (str.substr(str.length - 1) == ",")
str = str.substr(0, str.length - 1);
return str + "]";
break;
case Boolean:
return "\"" + String() + "\"";
break;
case Date:
return "\"" + String() + "\"";
break;
case Function:
break;
case Number:
return "\"" + String() + "\"";
break;
case String:
return "\"" + String() + "\"";
break;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论