C# JSON 转List类

因为项目需要,形成一个JSON帮助类

这是在.net3.5的环境下编写

除了要添加引用
System.Runtime.Serialization;

还要添加

System.ServiceModel

System.ServiceModel.Web


public class JsonHelper {
        /// 
        /// 把Json转成List
        /// 
        /// 
        /// 
        /// 
        public static IList JsonToList(string html) {
            IList result = new List();
            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)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)obj;
            }
            return result;
        }
        #region // 格式化Json字符串
        /// 
        /// 格式化Json字符串,使之能转换成List
        /// 
        /// 
        /// 
        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;
        }
        /// 
        /// 将Json序列化的时间由new Date(1373387734703)转为字符串"\/Date(1373387734703)\/"
        /// 
        private static string FormatJsonMatch(Match m) {
            return string.Format("\"\\/Date({0})\\/\"", m.Groups[2].Value);
        }

        #endregion // 格式化Json字符串

        #region // 格式化日期
        /// 
        /// 将Json序列化的时间由new Date(1373390933250) 或Date(1373390933250)或"\/Date(1373390933250+0800)\/"
        /// 转为2013-07-09 17:28:53
        /// 主要用于传给前台
        /// 
        /// 
        /// 
        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;

        }
        /// 
        /// 将Json序列化的时间由Date(1294499956278+0800)转为字符串
        /// 
        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 // 属性和变量转换
        /// 
        /// 属性转变量
        /// 把"
k__BackingField":"1", /// 转成"address":"1", ///
/// /// 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; } /// /// 变量转属性 /// 把"address":"1", /// 转成"
k__BackingField":"1", ///
/// /// 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 // 属性和变量转换 }

你可能感兴趣的:(asp.net,正则表达式)