首页 > 代码库 > 将前台JS数组序列化后提交,后台反序列化对象

将前台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 = http://www.mamicode.com/$thirdTd.find(":text:eq(0)").val();
    if ($.trim(entryData).length == 0) { alert(‘请输入第‘ + (i + 1) + ‘行要录入的数据!‘); return; }
    var _entry = {};
    _entry.EntryObjectID = entryObjectID;
    _entry.EntryData = http://www.mamicode.com/entryData;
    _entryArr.push(_entry);
    }
    var _data = http://www.mamicode.com/{};
    _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 = http://www.mamicode.com/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[]));

    1. //临时model类

    2. 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 (obj.constructor) {
            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 "\"" + obj.toString() + "\"";
            break;
            case Date:
            return "\"" + obj.toString() + "\"";
            break;
            case Function:
            break;
            case Number:
            return "\"" + obj.toString() + "\"";
            break;
            case String:
            return "\"" + obj.toString() + "\"";
            break;
        }
    }

将前台JS数组序列化后提交,后台反序列化对象