.NET MVC 序列化与反序列化

       using System.Runtime.Serialization.Json;

       using System.IO;

       using System.Text;

        //序列化
        public string MySerialize(object o)
        {
            //序列化
            DataContractJsonSerializer json = new DataContractJsonSerializer(o.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, o);
                string JsonData = Encoding.UTF8.GetString(stream.ToArray());
                return JsonData;
            }
        }

        $.ajax({
            type: "GET",
            url: "../product/GetProductList",
            data: {active:1},
            dataType: "json",
            success: function (data) {
                ProductList=data;
                var html = '';
                if (data != "") {
                    for (var i = 0; i < data.length; i++) {
                        html += "<tr>";
                        html += '<td style="font-size:18px;"><a onclick="ShowProductDetails('+data[i].FoodID+')">'+data[i].FoodName+'</a></td>'
                        html += "<td>约 " + data[i].Specifications + "</td>"
                        html += "<td style='text-align:center'>" + data[i].Price + "</td>"
                        html += '<td><label class="checkbox-inline"><input id="'+data[i].FoodID+'" type="checkbox" onclick="CheckboxClick('+data[i].FoodID+')" value="" style="zoom:300%; float:right;margin-top:-1px;margin-left:-6px;"></label></td>'
                        html += "</tr>";
                    }
                    $("#ProductList").empty();
                    $("#ProductList").html(html);
                }
            }
        });

         using System.IO;

         using System.Web.Script.Serialization;

        //反序列化
        public List<T> MyDeserialize<T>(HttpRequestBase request)
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            //反序列化
            JavaScriptSerializer js = new JavaScriptSerializer();
            var List = js.Deserialize<List<T>>(stream);
            return List;
        }

            $.ajax({
                type: "POST",
                url: "../order/Pay",
                data:JSON.stringify(Orders),
                dataType: "json",
                success: function (data) {
                    if (data != "") {
                        if(data.Code==200)
                        {
                            if(hb)
                            {
                                window.location.href = '../order/status?NO='+data.NO;
                            }
                            else
                            {
                                window.location.href = '../order/status?hb=false&NO='+data.NO;
                            }
                        }
                    }
                },
                error: function () {
                    alert("网络异常,请稍后再试···");
                }
            });

猜你喜欢

转载自www.cnblogs.com/lgq880821/p/10726787.html
今日推荐