程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# JSON 轉List<T>類

C# JSON 轉List<T>類

編輯:C#入門知識

因為項目需要,形成一個JSON幫助類

這是在.net3.5的環境下編寫

 

public class JsonHelper {
        /// <summary>
        /// 把Json轉成List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="html"></param>
        /// <returns></returns>
        public static IList<T> JsonToList<T>(string html) {
            IList<T> result = new List<T>();
            html = FormatJson(html);
            try {
                DataContractJsonSerializer _Json = new DataContractJsonSerializer(result.GetType());
                byte[] _Using = System.Text.Encoding.UTF8.GetBytes(html);
                System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
                _MemoryStream.Position = 0;
                object obj = _Json.ReadObject(_MemoryStream); ;
                result = (IList<T>)obj;
            }
            catch (Exception) {
                html = AttributeToVariable(html);
                DataContractJsonSerializer _Json = new DataContractJsonSerializer(result.GetType());
                byte[] _Using = System.Text.Encoding.UTF8.GetBytes(html);
                System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
                _MemoryStream.Position = 0;
                object obj = _Json.ReadObject(_MemoryStream); ;
                result = (IList<T>)obj;
            }
            return result;
        }
        #region // 格式化Json字符串
        /// <summary>
        /// 格式化Json字符串,使之能轉換成List
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string FormatJson(string value) {
            string p = @"(new Date)\(+([0-9,-]+)+(\))";
            MatchEvaluator matchEvaluator = new MatchEvaluator(FormatJsonMatch);
            Regex reg = new Regex(p);
            bool isfind = reg.IsMatch(value);
            value = reg.Replace(value, matchEvaluator);
            return value;
        }
        /// <summary>
        /// 將Json序列化的時間由new Date(1373387734703)轉為字符串"\/Date(1373387734703)\/"
        /// </summary>
        private static string FormatJsonMatch(Match m) {
            return string.Format("\"\\/Date({0})\\/\"", m.Groups[2].Value);
        }

        #endregion // 格式化Json字符串

        #region // 格式化日期
        /// <summary>
        /// 將Json序列化的時間由new Date(1373390933250) 或Date(1373390933250)或"\/Date(1373390933250+0800)\/"
        /// 轉為2013-07-09 17:28:53
        /// 主要用於傳給前台
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string FormatJsonDate(string value) {
            string p = @"(new Date)\(+([0-9,-]+)+(\))";
            MatchEvaluator matchEvaluator = new MatchEvaluator(FormatJsonDateMatch);
            Regex reg = new Regex(p);
            value = reg.Replace(value, matchEvaluator);

            p = @"(Date)\(+([0-9,-]+)+(\))";
            matchEvaluator = new MatchEvaluator(FormatJsonDateMatch);
            reg = new Regex(p);
            value = reg.Replace(value, matchEvaluator);

            p = "\"\\\\\\/" + @"Date(\()([0-9,-]+)([+])([0-9,-]+)(\))" + "\\\\\\/\"";
            matchEvaluator = new MatchEvaluator(FormatJsonDateMatch);
            reg = new Regex(p);
            value = reg.Replace(value, matchEvaluator);

            return value;

        }
        /// <summary>
        /// 將Json序列化的時間由Date(1294499956278+0800)轉為字符串
        /// </summary>
        private static string FormatJsonDateMatch(Match m) {

            string result = string.Empty;

            DateTime dt = new DateTime(1970, 1, 1);

            dt = dt.AddMilliseconds(long.Parse(m.Groups[2].Value));

            dt = dt.ToLocalTime();

            result = dt.ToString("yyyy-MM-dd HH:mm:ss");

            return result;
        }
        #endregion // 格式化日期

        #region // 屬性和變量轉換
        /// <summary>
        /// 屬性轉變量
        /// 把"<address>k__BackingField":"1",
        /// 轉成"address":"1",
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AttributeToVariable(string value) {
            string p = @"\<([A-Z,a-z,0-9,_]*)\>k__BackingField";
            MatchEvaluator matchEvaluator = new MatchEvaluator(AttributeToVariableMatch);
            Regex reg = new Regex(p);
            bool isfind = reg.IsMatch(value);
            value = reg.Replace(value, matchEvaluator);
            return value;
        }
        private static string AttributeToVariableMatch(Match m) {
            return m.Groups[1].Value;
        }

        /// <summary>
        /// 變量轉屬性
        /// 把"address":"1",
        /// 轉成"<address>k__BackingField":"1",
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string VariableToAttribute(string value) {
            string p = "\\\"([A-Z,a-z,0-9,_]*)\\\"\\:";
            MatchEvaluator matchEvaluator = new MatchEvaluator(VariableToAttributeMatch);
            Regex reg = new Regex(p);
            bool isfind = reg.IsMatch(value);
            value = reg.Replace(value, matchEvaluator);
            return value;
        }
        private static string VariableToAttributeMatch(Match m) {
            return string.Format("\"<{0}>k__BackingField\":", m.Groups[1].Value);
        }

        #endregion // 屬性和變量轉換


    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved