zoukankan      html  css  js  c++  java
  • ExensionJSON

    View Code
     public static class ExensionJSON
        {
            public static string ToJSONList(Type t,List<Object> source) 
            {
                string returnValue = "";
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                foreach (var item in source)
                {
                    sb.Append(ToJSON(t,item) + ",");
                }
                sb.Append("]");
                returnValue = sb.ToString();
                returnValue = Regex.Replace(returnValue, @",]", "]");
                return returnValue;
            }
    
            public static string ToJSON(Type t,Object source) 
            {
                string returnValue = "";
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                PropertyInfo[] properties = t.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    object propertyValue = property.GetValue(source, null);
                    if (propertyValue!=null && !String.IsNullOrWhiteSpace(propertyValue.ToString()))
                    {
                        sb.Append(string.Format("\"{0}\":\"{1}\",", property.Name, propertyValue));
                    }
                    else {
                        sb.Append(string.Format("\"{0}\":null,", property.Name));
                    }
                }
                sb.Append("}");
                returnValue = sb.ToString();
                returnValue = Regex.Replace(returnValue, @",}", "}");
                return returnValue;
            }
        }
  • 相关阅读:
    ADO.NET(一)数据库连接串的几种写法
    C#事件Event--猫捉老鼠
    事件
    委托
    C# .Net List<T>中Remove()、RemoveAt()、RemoveRange()、RemoveAll()的区别,List<T>删除汇总
    上传下载
    验证数据
    RSADemo2
    随机数
    二维码生成类
  • 原文地址:https://www.cnblogs.com/jiebian/p/3072944.html
Copyright © 2011-2022 走看看