C#中的DataSet、string、DataTable、对象转换成Json的实现代码
程序员文章站
2023-12-20 21:51:40
c#中对象,字符串,datatable、datareader、dataset,对象集合转换成json字符串方法。
public class convertjson...
c#中对象,字符串,datatable、datareader、dataset,对象集合转换成json字符串方法。
public class convertjson { #region 私有方法 /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="s">字符串</param> /// <returns>json字符串</returns> private static string string2json(string s) { stringbuilder sb = new stringbuilder(); for (int i = 0; i < s.length; i++) { char c = s.tochararray()[i]; switch (c) { 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(c); break; } } return sb.tostring(); } /// <summary> /// 格式化字符型、日期型、布尔型 /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> private static string stringformat(string str, type type) { if (type == typeof(string)) { str = string2json(str); str = "\"" + str + "\""; } else if (type == typeof(datetime)) { str = "\"" + str + "\""; } else if (type == typeof(bool)) { str = str.tolower(); } else if (type != typeof(string) && string.isnullorempty(str)) { str = "\"" + str + "\""; } return str; } #endregion #region list转换成json /// <summary> /// list转换为json /// </summary> /// <typeparam name="t"></typeparam> /// <param name="list"></param> /// <returns></returns> public static string listtojson<t>(ilist<t> list) { object obj = list[0]; return listtojson<t>(list, obj.gettype().name); } /// <summary> /// list转换为json /// </summary> /// <typeparam name="t1"></typeparam> /// <param name="list"></param> /// <param name="p"></param> /// <returns></returns> private static string listtojson<t>(ilist<t> list, string jsonname) { stringbuilder json = new stringbuilder(); if (string.isnullorempty(jsonname)) jsonname = list[0].gettype().name; json.append("{\"" + jsonname + "\":["); if (list.count > 0) { for (int i = 0; i < list.count; i++) { t obj = activator.createinstance<t>(); propertyinfo[] pi = obj.gettype().getproperties(); json.append("{"); for (int j = 0; j < pi.length; j++) { type type = pi[j].getvalue(list[i], null).gettype(); json.append("\"" + pi[j].name.tostring() + "\":" + stringformat(pi[j].getvalue(list[i], null).tostring(), type)); if (j < pi.length - 1) { json.append(","); } } json.append("}"); if (i < list.count - 1) { json.append(","); } } } json.append("]}"); return json.tostring(); } #endregion #region 对象转换为json /// <summary> /// 对象转换为json /// </summary> /// <param name="jsonobject">json对象</param> /// <returns>json字符串</returns> public static string tojson(object jsonobject) { string jsonstring = "{"; propertyinfo[] propertyinfo = jsonobject.gettype().getproperties(); for (int i = 0; i < propertyinfo.length; i++) { object objectvalue = propertyinfo[i].getgetmethod().invoke(jsonobject, 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 + ","; } jsonstring.remove(jsonstring.length - 1, jsonstring.length); return jsonstring + "}"; } #endregion #region 对象集合转换为json /// <summary> /// 对象集合转换为json /// </summary> /// <param name="array">对象集合</param> /// <returns>json字符串</returns> public static string tojson(ienumerable array) { string jsonstring = "{"; foreach (object item in array) { jsonstring += tojson(item) + ","; } jsonstring.remove(jsonstring.length - 1, jsonstring.length); return jsonstring + "]"; } #endregion #region 普通集合转换json /// <summary> /// 普通集合转换json /// </summary> /// <param name="array">集合对象</param> /// <returns>json字符串</returns> public static string toarraystring(ienumerable array) { string jsonstring = "["; foreach (object item in array) { jsonstring = tojson(item.tostring()) + ","; } jsonstring.remove(jsonstring.length - 1, jsonstring.length); return jsonstring + "]"; } #endregion #region dataset转换为json /// <summary> /// dataset转换为json /// </summary> /// <param name="dataset">dataset对象</param> /// <returns>json字符串</returns> public static string tojson(dataset dataset) { string jsonstring = "{"; foreach (datatable table in dataset.tables) { jsonstring += "\"" + table.tablename + "\":" + tojson(table) + ","; } jsonstring = jsonstring.trimend(','); return jsonstring + "}"; } #endregion #region datatable转换为json /// <summary> /// datatable转换为json /// </summary> /// <param name="table">datatable对象</param> /// <returns>json字符串</returns> public static string tojson(datatable dt) { 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 < dt.columns.count - 1) { jsonstring.append(strvalue + ","); } else { jsonstring.append(strvalue); } } jsonstring.append("},"); } jsonstring.remove(jsonstring.length - 1, 1); jsonstring.append("]"); return jsonstring.tostring(); } /// <summary> /// datatable转换为json /// </summary> public static string tojson(datatable dt, string jsonname) { stringbuilder json = new stringbuilder(); if (string.isnullorempty(jsonname)) jsonname = dt.tablename; json.append("{\"" + jsonname + "\":["); if (dt.rows.count > 0) { for (int i = 0; i < dt.rows.count; i++) { json.append("{"); for (int j = 0; j < dt.columns.count; j++) { type type = dt.rows[i][j].gettype(); json.append("\"" + dt.columns[j].columnname.tostring() + "\":" + stringformat(dt.rows[i][j].tostring(), type)); if (j < dt.columns.count - 1) { json.append(","); } } json.append("}"); if (i < dt.rows.count - 1) { json.append(","); } } } json.append("]}"); return json.tostring(); } #endregion #region datareader转换为json /// <summary> /// datareader转换为json /// </summary> /// <param name="datareader">datareader对象</param> /// <returns>json字符串</returns> 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(); } #endregion }
推荐阅读
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
C#中把Datatable转换为Json的5个代码实例
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
C#实现json格式转换成对象并更换key的方法
-
C#实现json格式转换成对象并更换key的方法
-
php实现数组中索引关联数据转换成json对象的方法
-
c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值
-
JS中的JSON对象的定义和取值实现代码
-
php实现数组中索引关联数据转换成json对象的方法
-
C#中的DataSet、string、DataTable 、对象转换成Json