码迷,mamicode.com
首页 > 编程语言 > 详细

一般处理程序页ashx 序列化 Json数组

时间:2015-06-19 16:33:40      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

json传递数组到一般处理程序页,序列化为实体类的方法,可以解决.ENT framework 3.5以前的项目,

3.5以后的项目可以用System.ServiceModel.Web和System.Runtime.Serialization解决,当然也可以使用这种方案。

 

下面是代码:

JS

技术分享
function PurchaseAutonomy() {
        var r = 0;
        var parmArr = new Array();
        $("#dataTb tbody tr").each(function () {
            var ch = $(this).children().eq(0).children().eq(0).attr("checked");
            var num = $(this).children().eq(7).children().eq(0).val();            
            if (ch == true)
            {
                if (IsNum(num)) {
                    return false;
                } else {
                    parmArr[r] = {
                        PRODUCT_ID: $(this).children().eq(1).text(),
                        PRODUCT_NAME: $(this).children().eq(3).text(),
                        ORG_ID: $(this).children().eq(0).children().eq(2).val(),
                        JS_PRICE: $(this).children().eq(0).children().eq(4).val(),
                        NOS_PRICE: $(this).children().eq(0).children().eq(3).val(),
                        PRODUCT_PRICE: $(this).children().eq(0).children().eq(4).val(),
                        PURCHASE_NUM: $(this).children().eq(7).children().eq(0).val()
                    };

                    r++;
                }
            }
        });
        
        if (r > 0) {

            $.MsgBox.Confirm("温馨提示", "生成预订单,确定继续吗?温馨提示", function () {
                var a = parmArr;
                $.ajax({
                    type: "POST",
                    url: "/DataServer/Purchase/PurchaseAdd.ashx",
                    data: { parmArr: JSON.stringify(parmArr),user_id:<%=user_id%>,bm_id:<%=bm_id%> },
                    datatype:"json",
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    success: function (result) {
                        if (result == "1") {
                            $.MsgBox.Alert("提示", "生成采购计划成功!");
                            $("#dataTb tbody tr input[type=‘checkbox‘]").attr("checked",false);
                            $("#dataTb tbody tr input[name=‘purchase_num‘]").val("0");
                        } else {
                            $.MsgBox.Alert("提示", "生成采购计划失败!");
                            return false;
                        }
                    }
                });

            });
        } else {
            $.MsgBox.Alert("提示", "请选择要处理的记录!");
            return false;
        }

    }
View Code

ASHX 画面:

引用:using Newtonsoft.Json;

接收数组字符串:

String parmArr = context.Request.Form["parmArr"];

序列化为实体类

List<ET_PURCHASE_ITEM> list = new List<ET_PURCHASE_ITEM>();
list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ET_PURCHASE_ITEM>>(parmArr);

实体类:

技术分享
/// <summary>
    /// ET_PURCHASE_ITEM:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public partial class ET_PURCHASE_ITEM
    {
        public ET_PURCHASE_ITEM()
        { }
        #region Model
        private decimal? _org_id;
        private decimal? _product_id;
        private string _product_name;
        private decimal? _js_price;
        private decimal? _nos_price;
        private decimal? _purchase_num=0;
        private decimal? _product_price;
        
        /// <summary>
        /// 
        /// </summary>
        public decimal? ORG_ID
        {
            set { _org_id = value; }
            get { return _org_id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public decimal? PRODUCT_ID
        {
            set { _product_id = value; }
            get { return _product_id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PRODUCT_NAME
        {
            set { _product_name = value; }
            get { return _product_name; }
        }
        /// <summary>
        /// 
        /// </summary>
        public decimal? JS_PRICE
        {
            set { _js_price = value; }
            get { return _js_price; }
        }
        /// <summary>
        /// 
        /// </summary>
        public decimal? NOS_PRICE
        {
            set { _nos_price = value; }
            get { return _nos_price; }
        }
        /// <summary>
        /// 
        /// </summary>
        public decimal? PURCHASE_NUM
        {
            set { _purchase_num = value; }
            get { return _purchase_num; }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public decimal? PRODUCT_PRICE
        {
            set { _product_price = value; }
            get { return _product_price; }
        }
        
        #endregion Model

    }
View Code

 

Newtonsoft.Json.dll 下载地址

http://files.cnblogs.com/files/shuaichao/Newtonsoft.Json.rar

一般处理程序页ashx 序列化 Json数组

标签:

原文地址:http://www.cnblogs.com/shuaichao/p/4588965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!