首页 > 代码库 > Asp.net常用开发方法之DataTable/DataReader转Json格式代码

Asp.net常用开发方法之DataTable/DataReader转Json格式代码


  1 public static string JsonParse(OleDbDataReader dataReader) //DataRead转json
  2         {
  3             StringBuilder jsonString = new StringBuilder();
  4             jsonString.Append("[");
  5             while (dataReader.Read())
  6             {
  7                 jsonString.Append("{");
  8                 for (int i = 0; i < dataReader.FieldCount; i++)
  9                 {
 10                     Type type = dataReader.GetFieldType(i);
 11                     string strKey = dataReader.GetName(i);
 12                     string strValue =http://www.mamicode.com/ dataReader[i].ToString();
 13                     jsonString.Append("\"" + strKey + "\":");
 14                     strValue =http://www.mamicode.com/ JsonFormat(strValue, type);
 15                     if (i < dataReader.FieldCount - 1)
 16                     {
 17                         jsonString.Append(strValue + ",");
 18                     }
 19                     else
 20                     {
 21                         jsonString.Append(strValue);
 22                     }
 23                 }
 24                 jsonString.Append("},");
 25             }
 26             dataReader.Close();
 27             jsonString.Remove(jsonString.Length - 1, 1);
 28             jsonString.Append("]");
 29             string rv = jsonString.ToString();
 30             if (rv == "]") { rv = ""; }
 31             return jsonString.ToString();
 32         }
 33 
 34         public static string JsonParse(DataTable dt)   //DataTable转json
 35         {
 36             DataRowCollection drc = dt.Rows;
 37             if (drc.Count == 0) { return ""; }
 38             StringBuilder jsonString = new StringBuilder();
 39             jsonString.Append("[");
 40             for (int i = 0; i < drc.Count; i++)
 41             {
 42                 jsonString.Append("{");
 43                 for (int j = 0; j < dt.Columns.Count; j++)
 44                 {
 45                     string strKey = dt.Columns[j].ColumnName;
 46                     string strValue =http://www.mamicode.com/ drc[i][j].ToString();
 47                     Type type = dt.Columns[j].DataType;
 48                     jsonString.Append("\"" + strKey + "\":");
 49                     strValue =http://www.mamicode.com/ JsonFormat(strValue, type);
 50                     if (j < dt.Columns.Count - 1)
 51                     {
 52                         jsonString.Append(strValue + ",");
 53                     }
 54                     else
 55                     {
 56                         jsonString.Append(strValue);
 57                     }
 58                 }
 59                 jsonString.Append("},");
 60             }
 61             jsonString.Remove(jsonString.Length - 1, 1);
 62             jsonString.Append("]");
 63             return jsonString.ToString();
 64         }
 65 
 66         public static string JsonFormat(string str, Type type)
 67         {
 68             if (type == typeof(string))
 69             {
 70                 StringBuilder sb = new StringBuilder();
 71                 for (int i = 0; i < str.Length; i++)
 72                 {
 73                     char c = str.ToCharArray()[i];
 74                     switch (c)
 75                     {
 76                         case \":
 77                             sb.Append("\\\""); break;
 78                         case \\:
 79                             sb.Append("\\\\"); break;
 80                         case /:
 81                             sb.Append("\\/"); break;
 82                         case \b:
 83                             sb.Append("\\b"); break;
 84                         case \f:
 85                             sb.Append("\\f"); break;
 86                         case \n:
 87                             sb.Append("\\n"); break;
 88                         case \r:
 89                             sb.Append("\\r"); break;
 90                         case \t:
 91                             sb.Append("\\t"); break;
 92                         default:
 93                             sb.Append(c); break;
 94                     }
 95                 }
 96                 str = sb.ToString();
 97                 str = "\"" + str + "\"";
 98             }
 99             else if (type == typeof(DateTime))
100             {
101                 if (IsDateTime(str))
102                 {
103                     DateTime dt = DateTime.Parse(str);
104                     str = "\"" + dt.GetDateTimeFormats(s)[0].ToString() + "\"";
105                 }
106                 else
107                 {
108                     str = "\"" + str + "\"";
109                 }
110                 // str = + str + ;
111             }
112             else
113             {
114                 str = "\"" + str + "\"";
115             }
116             return str;
117         }
118 public static bool IsDateTime(string str) //是否是日期格式(包含时间)
119         {
120             if (string.IsNullOrEmpty(str)) { return false; }
121             //日期
122             if (str.Trim().IndexOf(" ") < 0)
123             {
124                 return IsDate(str);
125             }
126             else
127             {
128                 string[] Astr = str.Split( ); //日期+时间
129                 if (Astr.Length == 2)
130                 {
131                     return IsDate(Astr[0]) && System.Text.RegularExpressions.Regex.IsMatch(Astr[1], @"^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
132                 }
133                 else
134                 {
135                     return false;
136                 }
137             }
138         }
139 
140         public static bool IsTime(string StrSource)
141         {
142             return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
143         }

 

 

技术分享

Asp.net常用开发方法之DataTable/DataReader转Json格式代码