1 public class ObjectToJson 2 { 3 /// <summary> 4 /// List转成json 5 /// </summary> 6 /// <typeparam name="T"></typeparam> 7 /// <param name="jsonName"></param> 8 /// <param name="list"></param> 9 /// <returns></returns> 10 public static string ListToJson<T>(IList<T> list, string jsonName) 11 { 12 StringBuilder Json = new StringBuilder(); 13 if (string.IsNullOrEmpty(jsonName)) 14 jsonName = list[0].GetType().Name; 15 Json.Append("{\"" + jsonName + "\":["); 16 if (list.Count > 0) 17 { 18 for (int i = 0; i < list.Count; i++) 19 { 20 T obj = Activator.CreateInstance<T>(); 21 PropertyInfo[] pi = obj.GetType().GetProperties(); 22 Json.Append("{"); 23 for (int j = 0; j < pi.Length; j++) 24 { 25 Type type = pi[j].GetValue(list[i], null).GetType(); 26 Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type)); 27 28 if (j < pi.Length - 1) 29 { 30 Json.Append(","); 31 } 32 } 33 Json.Append("}"); 34 if (i < list.Count - 1) 35 { 36 Json.Append(","); 37 } 38 } 39 } 40 Json.Append("]}"); 41 return Json.ToString(); 42 } 43 44 /// <summary> 45 /// List转成json 46 /// </summary> 47 /// <typeparam name="T"></typeparam> 48 /// <param name="list"></param> 49 /// <returns></returns> 50 public static string ListToJson<T>(IList<T> list) 51 { 52 object obj = list[0]; 53 return ListToJson<T>(list, obj.GetType().Name); 54 } 55 56 /// <summary> 57 /// 对象转换为Json字符串 58 /// </summary> 59 /// <param name="jsonObject">对象</param> 60 /// <returns>Json字符串</returns> 61 public static string ToJson(object jsonObject) 62 { 63 string jsonString = "{"; 64 PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); 65 for (int i = 0; i < propertyInfo.Length; i++) 66 { 67 object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null); 68 string value = string.Empty; 69 if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) 70 { 71 value = "'" + objectValue.ToString() + "'"; 72 } 73 else if (objectValue is string) 74 { 75 value = "'" + ToJson(objectValue.ToString()) + "'"; 76 } 77 else if (objectValue is IEnumerable) 78 { 79 value = ToJson((IEnumerable)objectValue); 80 } 81 else 82 { 83 value = ToJson(objectValue.ToString()); 84 } 85 jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ","; 86 } 87 jsonString.Remove(jsonString.Length - 1, jsonString.Length); 88 return jsonString + "}"; 89 } 90 91 /// <summary> 92 /// 对象集合转换Json 93 /// </summary> 94 /// <param name="array">集合对象</param> 95 /// <returns>Json字符串</returns> 96 public static string ToJson(IEnumerable array) 97 { 98 string jsonString = "["; 99 foreach (object item in array) 100 { 101 jsonString += ToJson(item) + ","; 102 } 103 jsonString.Remove(jsonString.Length - 1, jsonString.Length); 104 return jsonString + "]"; 105 } 106 107 /// <summary> 108 /// 普通集合转换Json 109 /// </summary> 110 /// <param name="array">集合对象</param> 111 /// <returns>Json字符串</returns> 112 public static string ToArrayString(IEnumerable array) 113 { 114 string jsonString = "["; 115 foreach (object item in array) 116 { 117 jsonString = ToJson(item.ToString()) + ","; 118 } 119 jsonString.Remove(jsonString.Length - 1, jsonString.Length); 120 return jsonString + "]"; 121 } 122 123 /// <summary> 124 /// Datatable转换为Json 125 /// </summary> 126 /// <param name="table">Datatable对象</param> 127 /// <returns>Json字符串</returns> 128 public static string ToJson(DataTable dt) 129 { 130 StringBuilder jsonString = new StringBuilder(); 131 jsonString.Append("["); 132 DataRowCollection drc = dt.Rows; 133 for (int i = 0; i < drc.Count; i++) 134 { 135 jsonString.Append("{"); 136 for (int j = 0; j < dt.Columns.Count; j++) 137 { 138 string strKey = dt.Columns[j].ColumnName; 139 string strValue = drc[i][j].ToString(); 140 Type type = dt.Columns[j].DataType; 141 jsonString.Append("\"" + strKey + "\":"); 142 strValue = StringFormat(strValue, type); 143 if (j < dt.Columns.Count - 1) 144 { 145 jsonString.Append(strValue + ","); 146 } 147 else 148 { 149 jsonString.Append(strValue); 150 } 151 } 152 jsonString.Append("},"); 153 } 154 jsonString.Remove(jsonString.Length - 1, 1); 155 jsonString.Append("]"); 156 return jsonString.ToString(); 157 } 158 159 /// <summary> 160 /// DataTable转成Json 161 /// </summary> 162 /// <param name="jsonName"></param> 163 /// <param name="dt"></param> 164 /// <returns></returns> 165 public static string ToJson(DataTable dt, string jsonName) 166 { 167 StringBuilder Json = new StringBuilder(); 168 if (string.IsNullOrEmpty(jsonName)) 169 jsonName = dt.TableName; 170 Json.Append("{\"" + jsonName + "\":["); 171 if (dt.Rows.Count > 0) 172 { 173 for (int i = 0; i < dt.Rows.Count; i++) 174 { 175 Json.Append("{"); 176 for (int j = 0; j < dt.Columns.Count; j++) 177 { 178 Type type = dt.Rows[i][j].GetType(); 179 Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type)); 180 if (j < dt.Columns.Count - 1) 181 { 182 Json.Append(","); 183 } 184 } 185 Json.Append("}"); 186 if (i < dt.Rows.Count - 1) 187 { 188 Json.Append(","); 189 } 190 } 191 } 192 Json.Append("]}"); 193 return Json.ToString(); 194 } 195 196 /// <summary> 197 /// DataReader转换为Json 198 /// </summary> 199 /// <param name="dataReader">DataReader对象</param> 200 /// <returns>Json字符串</returns> 201 public static string ToJson(DbDataReader dataReader) 202 { 203 StringBuilder jsonString = new StringBuilder(); 204 jsonString.Append("["); 205 while (dataReader.Read()) 206 { 207 jsonString.Append("{"); 208 for (int i = 0; i < dataReader.FieldCount; i++) 209 { 210 Type type = dataReader.GetFieldType(i); 211 string strKey = dataReader.GetName(i); 212 string strValue = dataReader[i].ToString(); 213 jsonString.Append("\"" + strKey + "\":"); 214 strValue = StringFormat(strValue, type); 215 if (i < dataReader.FieldCount - 1) 216 { 217 jsonString.Append(strValue + ","); 218 } 219 else 220 { 221 jsonString.Append(strValue); 222 } 223 } 224 jsonString.Append("},"); 225 } 226 dataReader.Close(); 227 jsonString.Remove(jsonString.Length - 1, 1); 228 jsonString.Append("]"); 229 return jsonString.ToString(); 230 } 231 232 /// <summary> 233 /// DataSet转换为Json 234 /// </summary> 235 /// <param name="dataSet">DataSet对象</param> 236 /// <returns>Json字符串</returns> 237 public static string ToJson(DataSet dataSet) 238 { 239 string jsonString = "{"; 240 foreach (DataTable table in dataSet.Tables) 241 { 242 jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ","; 243 } 244 jsonString = jsonString.TrimEnd(','); 245 return jsonString + "}"; 246 } 247 248 /// <summary> 249 /// 过滤特殊字符 250 /// </summary> 251 /// <param name="s"></param> 252 /// <returns></returns> 253 private static string String2Json(String s) 254 { 255 StringBuilder sb = new StringBuilder(); 256 for (int i = 0; i < s.Length; i++) 257 { 258 char c = s.ToCharArray()[i]; 259 switch (c) 260 { 261 case '\"': 262 sb.Append("\\\""); break; 263 case '\\': 264 sb.Append("\\\\"); break; 265 case '/': 266 sb.Append("\\/"); break; 267 case '\b': 268 sb.Append("\\b"); break; 269 case '\f': 270 sb.Append("\\f"); break; 271 case '\n': 272 sb.Append("\\n"); break; 273 case '\r': 274 sb.Append("\\r"); break; 275 case '\t': 276 sb.Append("\\t"); break; 277 default: 278 sb.Append(c); break; 279 } 280 } 281 return sb.ToString(); 282 } 283 284 /// <summary> 285 /// 格式化字符型、日期型、布尔型 286 /// </summary> 287 /// <param name="str"></param> 288 /// <param name="type"></param> 289 /// <returns></returns> 290 private static string StringFormat(string str, Type type) 291 { 292 if (type == typeof(string)) 293 { 294 str = String2Json(str); 295 str = "\"" + str + "\""; 296 } 297 else if (type == typeof(DateTime)) 298 { 299 str = "\"" + str + "\""; 300 } 301 else if (type == typeof(bool)) 302 { 303 str = str.ToLower(); 304 } 305 return str; 306 } 307 }
标签:jsonString,string,C#,Json,JSON,ToString,格式,dt,Append From: https://www.cnblogs.com/lgx5/p/18305477