• .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("网络异常,请稍后再试···");
                    }
                });

  • 相关阅读:
    树莓派使用MJPG-Streamer实现网络监控
    树莓派USB摄像头与camera模块对比
    机器人教程
    win10开始菜单打不开怎么办 win菜单键没反应解决办法
    solr查询语法
    Substance 6 设置 watermark(水印)
    在SWING里嵌入SWT的组件
    solr 5.5.1安装并配置中文分词IKAnalyzer
    [简单]docx4j常用方法小结
    Java串口通信详解
  • 原文地址:https://www.cnblogs.com/lgq880821/p/10726787.html
Copyright © 2020-2023  润新知