前台给后台传JSON字符串,后台解析并保存

Posted 努力向上的小蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前台给后台传JSON字符串,后台解析并保存相关的知识,希望对你有一定的参考价值。

 

前台

  1 function BMSure() {
  2             var DanWeiName = $("[id$=\'BusinessName\']").val();
  3             var Address = $("[id$=\'Address\']").val();
  4             if ((DanWeiName != "") && (Address != "")) {
  5                 var planGuid = $("[id$=\'hid_planGuid\']").val();
  6                 //var displayname = $("[id$=\'hid_displayName\']").val();
  7                 var num = $("[id$=\'hid_sum\']").val();
  8                 $.ajax({
  9                     type: "POST",
 10                     contentType: "application/json;charset=utf-8",
 11                    
 12                     url: "../Ashx/SaveBMInfo.ashx?PlanGuid=" + planGuid,
 13                     data: JSON.stringify(GetJsonData()),
 14                     dataType: "json",
 15                     success: function (message) {
 16                         if (message == 1) {
 17                            
 18                             ConfirmBM();
 19                             //ReturnLogin(DanWeiName);
 20                            
 21                         }
 22                     },
 23                     error: function (message) {
 24                         if (message == 0) {
 25                             layer.msg("保存失败");
 26                         }
 27                     }
 28                 })
 29             }
 30             else {
 31                 layer.msg("请将单位信息填写完整");
 32                 return false;
 33             }
 34         };
 35 
 36 function ReturnLogin(DanWeiName) {//返回登录名密码
 37             layer.open({
 38                 type: 2,
 39                 title: "提示信息",
 40                 area: [\'400px\', \'220px\'],
 41                 content: [\'LoginID_PassWord.aspx?DanWeiName=\' + DanWeiName],
 42                 skin: \'layer-ext-iframe\',
 43                 //关闭按钮刷新父页面
 44                 cancel: function () {
 45                     window.location.reload();
 46                 }
 47             });
 48         }
 49 
 50         function ConfirmBM() {
 51             //是否继续报名
 52             layer.confirm(\'报名成功,是否继续报名?\',
 53                 {
 54                     skin: \'layui-layer-molv\', btn: [\'继续报名\', \'关闭\'] //按钮
 55                 }, function (index) {
 56                     layer.close(index);
 57                     window.location.reload();
 58                 }, function (index) {
 59                     layer.close(index);
 60 
 61                     var index2 = parent.layer.getFrameIndex(window.name);
 62                     parent.layer.close(index2);//关闭弹出层
 63                 });
 64 
 65         }
 66 
 67 function GetJsonData() {
 68             var temp = $("[id$=\'hid_sum\'").val();//添加人员的数量
 69             var json_person = \'\';
 70             for (var i = 1; i <= temp; i++) {
 71                 var text = "";
 72                 $("[id$=\'addPerson" + i + "\']").find("td").children("input").each(function () {
 73                     text += $(this).val() + ",";
 74                 })
 75                 var a = new Array();
 76                 a = text.split(\',\');
 77                 if (a != "" && a != null) {
 78                     var json_persontemp = {
 79                         "Name": a[0],
 80                         "Sex": a[1],
 81                         "Identity": a[2],
 82                         "Brithday": a[3],
 83                         "Phone": a[4]
 84                     }
 85                 }
 86                 if (i == temp)
 87                     json_person += JSON.stringify(json_persontemp);
 88                 else
 89                     json_person += JSON.stringify(json_persontemp) + \',\';
 90 
 91             };
 92 
 93 
 94             var persontemp = eval(\'[\' + json_person + \']\');
 95 
 96             var json = {
 97                 "DanWeiName": $("[id$=\'BusinessName\']").val(),
 98                 "DetailAddress": $("[id$=\'Address\']").val(),
 99                 "BelongFenJu": $("[id$=\'BelongFenJu\']").val(),
100                 "PersonData":
101                  //JSON.parse(json_person)
102                   persontemp
103 
104             };
105 
106 
107 
108             return json;
109         }
View Code

后台(此处 使用SaveBMInfo 一般处理程序,后台直接写也一样)

  1  public class SaveBMInfo : IHttpHandler
  2     {
  3         Epoint.KSPXBase.Bizlogic.BLL.B_PK_Unit b_unit = new Epoint.KSPXBase.Bizlogic.BLL.B_PK_Unit();
  4         Epoint.KSPXBase.Bizlogic.BLL.B_PK_User b_user = new Epoint.KSPXBase.Bizlogic.BLL.B_PK_User();
  5         Epoint.PeiXun.Bizlogic.BLL.PXBaoM.B_PX_BaoM b_baom = new Epoint.PeiXun.Bizlogic.BLL.PXBaoM.B_PX_BaoM();
  6         M_PK_Unit m_unit = new M_PK_Unit();
  7         M_PK_User m_user = new M_PK_User();
  8         M_PX_BaoM m_baom = new M_PX_BaoM();
  9 
 10         List<PersonDataJSON> list_PersonData = new List<PersonDataJSON>();
 11         public void ProcessRequest(HttpContext context)
 12         {
 13             string result = "";
 14             context.Response.ContentType = "application/json";
 15             string PlanGuid = context.Request.QueryString["PlanGuid"].ToString();
 16 
 17             var data = context.Request;
 18             var sr = new StreamReader(data.InputStream);
 19             var stream = sr.ReadToEnd();
 20             var javascriptSerializer = new JavaScriptSerializer();
 21             var PostedData = javaScriptSerializer.Deserialize<RequestDataJSON>(stream);
 22 
 23             try
 24             {
 25                 #region
 26                 if (PostedData != null)
 27                 {
 28                     m_unit.RowGuid = Guid.NewGuid().ToString();//单位唯一标识
 29                     m_unit.DanWeiName = PostedData.DanWeiName;//单位名称
 30                     m_unit.DetailAddress = PostedData.DetailAddress;//单位地址
 31                     m_unit.BelongFenJu = PostedData.BelongFenJu;//所属分局  --报名点
 32                     m_unit.LoginID = ConvertToPY(PostedData.DanWeiName);//登录名
 33                     m_unit.Password = common.authPassword("111111");//密码
 34                     m_unit.AddType = "1";//报名方式  1-前台报名 2-后台添加 3-接口获取
 35                     m_unit.UserType = "1";//会员类别 0-个人 1-企业 2-学校 3-培训机构 
 36                     m_unit.IsEnable = "1";
 37                     m_unit.DelFlag = "0";
 38                     m_unit.Status = "2";//会员状态
 39                     b_unit.Insert(m_unit);
 40 
 41 
 42                     var peopleInfo = PostedData.PersonData;
 43                     for (int i = 0; i < peopleInfo.Count; i++)
 44                     {
 45                         m_user.RowGuid = Guid.NewGuid().ToString();//人员唯一标识
 46                         m_user.LoginID = PostedData.PersonData[i].Identity;//登录名为身份证号码
 47                         m_user.Password = common.authPassword("111111");//密码
 48                         m_user.DanWeiGuid = m_unit.RowGuid;//单位Guid
 49                         m_user.DanWeiName = m_unit.DanWeiName;//单位名称
 50                         m_user.Name = PostedData.PersonData[i].Name;//人员姓名
 51                         if (PostedData.PersonData[i].Sex == "")//性别
 52                             m_user.Sex = "0";
 53                         else
 54                             m_user.Sex = "1";
 55                         m_user.IdentityNum = PostedData.PersonData[i].Identity;//身份证号
 56                         m_user.BirthDay =Convert.ToDateTime(PostedData.PersonData[i].Brithday);//出生日期
 57                         m_user.MobilePhone = PostedData.PersonData[i].Phone;//手机号
 58                         m_user.AddType = "2";//帐号添加方式 1-注册 2-企业添加 3-后台添加
 59                         m_user.UserType = "0";//会员类别 0-个人 1-企业 2-学校 3-培训机构 
 60                         m_user.IsEnable = "1";
 61                         m_user.DelFlag = "0";
 62                         m_user.Status = "2";//会员状态
 63                         m_user.IdentityType = "111";//身份证
 64                         b_user.Insert(m_user);
 65 
 66                         m_baom.RowGuid = Guid.NewGuid().ToString();//报名唯一标识
 67                         m_baom.BMType = "2";//报名类别
 68                         m_baom.AddDate = m_baom.OperateDate = DateTime.Now;//报名日期
 69                         m_baom.Name = m_user.Name;//姓名
 70                         m_baom.LoginID = m_user.LoginID;//登录名
 71                         m_baom.UserGuid = m_user.RowGuid;//人员Guid
 72                         m_baom.PlanGuid = PlanGuid;//计划Guid
 73                         m_baom.DanWeiGuid = m_unit.RowGuid;//单位Guid
 74                         m_baom.DanWeiName = m_unit.DanWeiName;//单位名称
 75                         m_baom.BaoMDNum = m_unit.BelongFenJu;//报名点 --所属分局
 76                         b_baom.Insert(m_baom);
 77                     }
 78                 }
 79                 #endregion
 80                 result = "1";
 81             }
 82             catch (Exception ex)
 83             {
 84                 result = "0";
 85             }
 86             context.Response.ContentType = "text/plain";
 87             context.Response.Write(result);
 88         }
 89 
 90         public bool IsReusable
 91         {
 92             get
 93             {
 94                 return false;
 95             }
 96         }
 97 
 98         public string ConvertToPY(string Text)
 99         {
100             char[] chText = Text.ToCharArray();
101             string pyText = "";
102             foreach (char ch in chText)
103             {
104                 pyText += new ChineseChar(ch).Pinyins[0].Substring(0, 1);
105             }
106             return pyText;
107         }
108 
109         public class RequestDataJSON
110         {
111             public string DanWeiName { get; set; }
112             public string DetailAddress { get; set; }
113             public string BelongFenJu { get; set; }
114             public List<PersonDataJSON> PersonData;
115 
116         }
117         public class PersonDataJSON
118         {//人员
119             public string Name { get; set; }
120             public string Sex { get; set; }
121             public string Identity { get; set; }
122             public string Brithday { get; set; }
123             public string Phone { get; set; }
124         }
125     }
View Code

 

以上是关于前台给后台传JSON字符串,后台解析并保存的主要内容,如果未能解决你的问题,请参考以下文章

java后台怎么接收并解析前台传过来的json

页面同时传多条数据到后台并保存到数据库

java中如何在前台jsp页面封装一个map并传到后台啊

java后台怎么接收并解析前台传过来的json-CSDN论坛

公司动态添加行前台穿一个json的字符串到后台,并解析

js前台页面与后台如何传参