前端用JQ生成JSON字符串

            var num=$.MvcSheetUI.GetElement("OTC09_01").SheetGridView().RowCount; //子表行数
            var jsonobj = {}; //定义JSON
            jsonobj.rows=[];  //定义成员数组
            for(var i=1;i<=num;i++)
            {
                var obj={}; //定义数组成员
                obj.qty=$.MvcSheetUI.GetControlValue("OTC09_01.BGSL",i);
                obj.predate=$.MvcSheetUI.GetControlValue("OTC09_01.BGRQ",i);
                obj.socode=$.MvcSheetUI.GetControlValue("OTC09_01.DDH",i);
                obj.rowno=$.MvcSheetUI.GetControlValue("OTC09_01.RowNo",i);
                jsonobj.rows.push(obj);  //把成员加入数组
            }
            $.MvcSheetUI.SetControlValue("jsonobj",JSON.stringify(jsonobj));   //以JSON格式转成字符串

C#接收到解析

        public WSResult OTC09ToERP(string jsonStr)
        {
            //提取JSON行数组
            JObject jsonObj = (JObject)JsonConvert.DeserializeObject(jsonStr);
            JArray jsonArr = (JArray)JsonConvert.DeserializeObject(jsonObj["rows"].ToString());
            int num = jsonArr.Count;
            //构建ERP SQL字符串
            string sqlTests = string.Empty;
            for (var i = 0; i < num; i++)
            {
                JObject jsonRow = (JObject)JsonConvert.DeserializeObject(jsonArr[i].ToString());
                //变更数量,件数,发货日期,完工日期
                string sqlTest = @"update SO_SODetails set iQuantity={0},iNum={0}/(ISNULL(b.cInvDefine13,1)),dPreDate={1},dPreMoDate={1},cScloser={4} from SO_SODetails a
                                inner join inventory b on a.cInvCode=b.cInvCode
                                where a.cSOCode='{2}' and a.iRowNo={3} ";
                //处理空值
                string val1, val2,val3 = val1 = val2 = val3 = string.Empty;
                val1 = jsonRow["qty"].ToString() == "" ? "iQuantity" : jsonRow["qty"].ToString();
                val2 = jsonRow["predate"].ToString() == "" ? "dPreDate" : "'" + jsonRow["predate"].ToString() + "'";
                val3 = jsonRow["qty"].ToString() == "0" ? "'EAI'" : "cScloser";
                //格式化占位符
                sqlTests += string.Format(sqlTest, val1, val2, jsonRow["socode"].ToString(), jsonRow["rowno"].ToString(),val3);
            }
            //执行ERP变更
            SqlCon con = new SqlCon("ERP");
            try
            {
                con.open();
                if (con.ENQuery(sqlTests) > 0)
                {
                    r._message = "订单变更成功";
                    r._result = "1";
                    r._state = "successful";
                }

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.close();
            }
            return r;
        }

你可能感兴趣的:(前端用JQ生成JSON字符串)