直接贴代码:
1 using System; 2 using System.Data; 3 using System.Text; 4 using System.Collections.Generic; 5 using System.Reflection; 6 7 namespace PowerCheck.Tools 8 { 9 10 public class ToJson 11 { 12 public ToJson() 13 { 14 15 } 16 public static string DataTableToJson(string jsonName, DataTable dt) 17 { 18 StringBuilder Json = new StringBuilder(); 19 Json.Append("{\"" + jsonName + "\":["); 20 if (dt.Rows.Count > 0) 21 { 22 for (int i = 0; i < dt.Rows.Count; i++) 23 { 24 Json.Append("{"); 25 for (int j = 0; j < dt.Columns.Count; j++) 26 { 27 StringBuilder sb = new StringBuilder(); 28 escape(dt.Rows[i][j].ToString(),sb); 29 30 Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + sb.ToString() + "\""); 31 if (j < dt.Columns.Count - 1) 32 { 33 Json.Append(","); 34 } 35 } 36 Json.Append("}"); 37 if (i < dt.Rows.Count - 1) 38 { 39 Json.Append(","); 40 } 41 } 42 } 43 Json.Append("]}"); 44 return Json.ToString(); 45 } 46 47 /// <summary> 48 /// 将string数组转换成json字符串 49 /// </summary> 50 /// <param name="TagStrings">json对象数组名称</param> 51 /// <param name="Strings">json对象数组值</param> 52 /// <returns></returns> 53 public static string StringArrayToJason(string[] TagStrings, string[] Strings) 54 { 55 StringBuilder Json = new StringBuilder(); 56 Json.Append("{"); 57 for (int i = 0; i < Strings.Length; i++) 58 { 59 Json.Append(TagStrings[i]); 60 Json.Append(":"); 61 Json.Append(Strings[i]); 62 if (i < Strings.Length - 1) 63 { 64 Json.Append(","); 65 } 66 } 67 Json.Append("}"); 68 return Json.ToString(); 69 } 70 71 public static string ObjectToJson<T>(string jsonName, IList<T> IL) 72 { 73 StringBuilder Json = new StringBuilder(); 74 Json.Append("{\"" + jsonName + "\":["); 75 if (IL.Count > 0) 76 { 77 for (int i = 0; i < IL.Count; i++) 78 { 79 T obj = Activator.CreateInstance<T>(); 80 Type type = obj.GetType(); 81 PropertyInfo[] pis = type.GetProperties(); 82 Json.Append("{"); 83 for (int j = 0; j < pis.Length; j++) 84 { 85 Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\""); 86 if (j < pis.Length - 1) 87 { 88 Json.Append(","); 89 } 90 } 91 Json.Append("}"); 92 if (i < IL.Count - 1) 93 { 94 Json.Append(","); 95 } 96 } 97 } 98 Json.Append("]}"); 99 return Json.ToString(); 100 } 101 102 103 public static void escape(string s, StringBuilder sb) 104 { 105 106 for (int i = 0; i < s.Length; i++) 107 { 108 char ch = s[i]; 109 110 switch (ch) 111 { 112 113 case '"': 114 sb.Append("\\\""); 115 break; 116 117 case '\\': 118 119 sb.Append("\\\\"); 120 121 break; 122 123 case '\b': 124 125 sb.Append("\\b"); 126 127 break; 128 129 case '\f': 130 131 sb.Append("\\f"); 132 133 break; 134 135 case '\n': 136 137 sb.Append("\\n"); 138 139 break; 140 141 case '\r': 142 143 sb.Append("\\r"); 144 145 break; 146 147 case '\t': 148 149 sb.Append("\\t"); 150 151 break; 152 153 case '/': 154 155 sb.Append("\\/"); 156 157 break; 158 159 default: 160 161 // Reference: http://www.unicode.org/versions/Unicode5.1.0/ 162 163 if ((ch >= '\u0000' && ch <= '\u001F') 164 165 || (ch >= '\u007F' && ch <= '\u009F') 166 167 || (ch >= '\u2000' && ch <= '\u20FF')) 168 { 169 170 string ss = Convert.ToString(ch,16); 171 172 sb.Append("\\u"); 173 174 for (int k = 0; k < 4 - ss.Length; k++) 175 { 176 177 sb.Append('0'); 178 179 } 180 181 sb.Append(ss.ToUpper()); 182 183 } 184 else 185 { 186 187 sb.Append(ch); 188 189 } 190 191 break; 192 193 } 194 195 } 196 197 } 198 199 } 200 201 202 203 }
时间: 2024-10-25 08:05:47