js前端构造json对象后台接收并反序列化

<script type="text/javascript">
                $(function () {
                    $("button#ljyd").click(function () {
                        var errorMsg = 0;
                        //会员ID
                        var memberId = $("#memberId").val();
                        //联盟套餐Id
                        var unionId = $("#unionHideId").val();
                        var txt = [];
  
                        $("div.lmsj_bt.lmyd_0").each(function () {
                            var unionContentId = $(this).attr("data-unionContentId");
                            var contentCompanyId = $(this).attr("data-contentCompanyId");
                            var companyName = $(this).attr("data-contentCompanyName");


                            var tabl = $(this).find("a.dcbj_1 input:hidden");
                            //选择的桌子
                            var tableId = tabl.val() == undefined ? null : tabl.val();
                            //选择的菜品
                            var proIds = "";
                            $(this).find("div.plus-tag.tagbtn.clearfix a").each(function () {
                                proIds += $(this).attr("value") + ",";
                            });
                            if (proIds.length != 0) {
                                proIds = proIds.substring(0, proIds.length - 1);
                            }
                            //选择的时间
                            var time = $(this).find("#datepicker").val() + " " + $(this).find("select").val();
                            if ($(this).find("#datepicker").val() == "" || $(this).find("select").val() == "") {
                                //new Boxy.alert(companyName + "的预定时间必须选择完整", null, { title: "提示信息" });
                                errorMsg = 1;
                                return;
                            }
                            //预定人数
                            var people = $(this).find("div.lmsj_renshu.float_2 input").val();
                            if (people == "") {
                                //new Boxy.alert(companyName + "的预定人数必须填写", null, { title: "提示信息" });
                                errorMsg = 2;
                                return;
                            }

                            //alert(unionContentId + " " + contentCompanyId + " " + tableId + " " + proIds + " " + time + " " + people);
                            var obj = { "unionContentId": unionContentId, "contentCompanyId": contentCompanyId, "tableId": tableId, "proIds": proIds, "time": time, "people": people }//构造Json
                            txt.push(obj);
                        });
                        //联系人
                        var ydpepole = $("#ydmemberName").val();
                        //联系人电话
                        var ydpeoplephone = $("#ydmemberPhone").val();
                        if (errorMsg==0) {
                            $.ajax({
                                type: "get",
                                url: "/CompanyZxyd/UnionNewAdd",
                                data: "UnionContent=" + JSON.stringify(txt) + "&memberId=" + memberId + "&unionId=" + unionId + "&ydpepole=" + ydpepole + "&ydpeoplephone=" + ydpeoplephone,
                                success: function (msg) {
                                    new Boxy.alert(msg, null, { title: "提示信息" }); 
                                    //预定成功后的操作
                                }   //操作成功后的操作!msg是后台传过来的值
                            });
                        }
                        else {
                            if (errorMsg==1) {
                                new Boxy.alert( "预定时间必须选择完整", null, { title: "提示信息" });
                            } else {
                                new Boxy.alert("预定人数必须填写", null, { title: "提示信息" });
                            }
                        }

                    });

                })
            </script>

这是前台构造json对象的代码

[DataContract]//构造一个对象模型
    class Person
    {
        [DataMember]
        public string unionContentId { get; set; }
        [DataMember]
        public string contentCompanyId { get; set; }
        [DataMember]
        public string tableId { get; set; }
        [DataMember]
        public string proIds { get; set; }
        [DataMember]
        public string time { get; set; }
        [DataMember]
        public string people { get; set; }
    }

/// <summary>
    /// JSON序列化和反序列化辅助类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// JSON序列化
        /// </summary>
        public static string JsonSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }

        /// <summary>
        /// JSON反序列化
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(ms);
            return obj;
        }
    }
//调用方法
List<Person> outPerson = new List<Person>();
outPerson = JsonHelper.JsonDeserialize<List<Person>>(Request.QueryString["UnionContent"]);

这是后台处理代码

 

转载于:https://www.cnblogs.com/setManager/p/3200427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值