zoukankan      html  css  js  c++  java
  • 将json字符串转换成list<T>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using System.IO;
    using System.Text;
    namespace table
    {
        /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>
        // [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class Ttable : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //context.Response.Write("Hello World");
                //反射的机制:只需要传方法进来就可以执行方法、获得数据。。
                if (context.Request["ajaxMethod"] != null)
                {
                    string getMethod = context.Request["ajaxMethod"];
                    var method = this.GetType().GetMethod(getMethod);
                    if (method != null)
                    {
                        method.Invoke(this, new object[] { context });
                    }
                }

            }
            /// <summary>
            /// 序列化的Json字符串
            /// </summary>
            public void getInitData(HttpContext context)
            {

                List<SupplierEty> list = new List<SupplierEty>();
                SupplierEty enty1 = new SupplierEty
                {
                    SupplierID = 1111,
                    ContractName = "test",
                    ContractTitle = "test"
                };
                SupplierEty enty2 = new SupplierEty
                {
                    SupplierID = 2222,
                    ContractName = "test2",
                    ContractTitle = "test2"
                };
                SupplierEty enty3 = new SupplierEty
                {
                    SupplierID = 3333,
                    ContractName = "test3",
                    ContractTitle = "test3"
                };
                list.Add(enty1);
                list.Add(enty2);
                list.Add(enty3);
                string strJson = JsonHelper.GetJson<List<SupplierEty>>(list);
                context.Response.Write(strJson);
            }

            public void saveData(HttpContext context)
            {
                List<Cety> listety = null;
                string jsonText = context.Request["data"];
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Cety>));

                try
                {
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText)))
                    {

                       listety = (List<Cety>)serializer.ReadObject(ms);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            public class Cety {
                public string SupplierID;
                public string ContractName;
                public string ContractTitle;
            
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

  • 相关阅读:
    MySQL配置文件mysql.ini参数详解、MySQL性能优化
    破解许可
    mysql本地可以访问 网络不能访问
    解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
    MYSQL数据库如何赋予远程某个IP访问权限
    linux网络编程 no route to host 解决方案
    CentOS添加路由表
    CentOS安装zip unzip命令
    Centos7下的systemctl命令与service和chkconfig
    header头参数不能带下划线
  • 原文地址:https://www.cnblogs.com/cxlings/p/3486533.html
Copyright © 2011-2022 走看看