C# Datatable、DataReader等转化json

网友投稿 717 2022-10-06

C# Datatable、DataReader等转化json

C# Datatable、DataReader等转化json

//对象转换为Json字符串 public static string ToJson(object jsonObject) { object objectValue = string.Empty; string jsonString = "{"; if (jsonObject==null) { return jsonString + "}"; } PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); for (int i = 0; i < propertyInfo.Length; i++) { try { objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, BindingFlags.InvokeMethod, null, new object[] { }, null); string value = string.Empty; if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) { value = "'" + objectValue.ToString() + "'"; } else if (objectValue is string) { value = "'" + ToJson(objectValue.ToString()) + "'"; } else if (objectValue is IEnumerable) { value = ToJson((IEnumerable)objectValue); } else { value = ToJson(objectValue.ToString()); } jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ","; } catch (Exception ex) { string s = ex.Message; } } jsonString.Remove(jsonString.Length - 1); return jsonString + "}"; } //对象集合转换Json public static string ToJson(IEnumerable array) { if (array == null) return ""; string jsonString = "["; foreach (object item in array) { jsonString+= ToJson(item) + ","; } jsonString.Remove(jsonString.Length - 1); return jsonString + "]"; } //普通集合转换Json public static string ToArrayString(IEnumerable array) { if (array == null) return ""; string jsonString = "["; foreach (object item in array) { jsonString = ToJson(item.ToString()) + ","; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "]"; } //Datatable转换为Json public static string ToJson(DataTable dt) { if (dt == null || dt.Rows.Count == 0) return "[{}]"; StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); DataRowCollection drc = dt.Rows; for (int i = 0; i < drc.Count; i++) { jsonString.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; string strValue = drc[i][j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append("\"" + strKey + "\":"); strValue = StringFormat(strValue, type); if(j 0) { for (int i = 0; i < dt.Rows.Count; i++) { sb.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Type type = dt.Rows[i][j].GetType(); sb.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type)); if (j < dt.Columns.Count - 1) { sb.Append(","); } } sb.Append("}"); if (i < dt.Rows.Count - 1) { sb.Append(","); } } } sb.Append("]}"); return sb.ToString(); } //DataReader转换为Json public static string ToJson(DbDataReader dataReader) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); while (dataReader.Read()) { jsonString.Append("{"); for (int i = 0; i < dataReader.FieldCount; i++) { Type type = dataReader.GetFieldType(i); string strKey = dataReader.GetName(i); string strValue = dataReader[i].ToString(); jsonString.Append("\"" + strKey + "\":"); strValue = StringFormat(strValue, type); if (i < dataReader.FieldCount - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } dataReader.Close(); jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); return jsonString.ToString(); } //DataSet转换为Json public static string ToJson(DataSet dataSet) { string jsonString = "{"; foreach (DataTable table in dataSet.Tables) { jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ","; } jsonString = jsonString.TrimEnd(','); return jsonString + "}"; } //ArrayList转化为json public static string ArryListToJson(ArrayList alist, string jsonName) { string json = string.Empty; string strSubList = "\"" + jsonName + "\":" + "[{"; for (int i = 0; i < alist.Count; i++) { Hashtable ht1 = (Hashtable)alist[i]; foreach (DictionaryEntry de in ht1) { if (de.Value is ArrayList) { strSubList += ArryListToJson((ArrayList)de.Value, de.Key.ToString()); strSubList += "]"; } else { strSubList += "\"" + de.Key + "\":" + "\"" + de.Value + "\","; } } strSubList = strSubList.Remove(strSubList.Length - 1); strSubList += "},{"; } if (strSubList.Length >= 2) { json = strSubList.Remove(strSubList.Length - 2); } json += "]"; return json; } //Hashtable To Json public static string HashtableToJson(Hashtable hr) { string json = "{"; foreach (DictionaryEntry row in hr) { try { string key = "\"" + row.Key + "\":"; string value = string.Empty; if (row.Value is ArrayList) { key = string.Empty; value = ArryListToJson((ArrayList)row.Value, row.Key.ToString()); value += ","; } else { value = "\"" + (row.Value == null ? "" : row.Value.ToString()) + "\","; } json += key + value; } catch (Exception ex) { string mess = ex.Message; } } json = json.Remove(json.Length - 1); json = json + "}"; return json; } //转换字符格式 private static string StringFormat(string str, Type type) { if (string.IsNullOrEmpty(str)) return "\"\""; if (type == typeof(string)) { str = "\"" + ConvertStrToSpecificationStrs(str) + "\""; } else if (type == typeof(DateTime)) { str = "\"" + str + "\""; } else if (type == typeof(bool)) { str = str.ToLower(); } return str; } //转换字符格式 public static string ConvertStrToSpecificationStrs(string str) { if (string.IsNullOrEmpty(str)) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.Length; i++) { char cStr = str.ToCharArray()[i]; switch (cStr) { case '\"': sb.Append("\\""); break; case '\': sb.Append("\\"); break; case '/': sb.Append("\/"); break; case '\b': sb.Append("\b"); break; case '\f': sb.Append("\f"); break; case '\n': sb.Append("\n"); break; case '\r': sb.Append("\r"); break; case '\t': sb.Append("\t"); break; default: sb.Append(cStr); break; } } return sb.ToString(); }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序通过保存图片分享到朋友圈的功能实现(小程序怎么分享图片到朋友圈)
下一篇:oracle学习笔记(一)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~