將json字串轉換成list<T>

(二少)在南極發表於2013-12-22

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

相關文章