将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;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/cxlings/p/3486533.html