C#实现DataTable,List和Json转换的方法
程序员文章站
2023-08-26 23:40:46
本文实例讲述了c#实现datatable,list和json转换的方法。分享给大家供大家参考,具体如下:
1. 将datatable或ilist<>转换成js...
本文实例讲述了c#实现datatable,list和json转换的方法。分享给大家供大家参考,具体如下:
1. 将datatable或ilist<>转换成json格式
using system; using system.data; using system.text; using system.collections.generic; using system.reflection; /// <summary> /// 将datatable或ilist<>转换成json格式 /// </summary> public class tojson { public tojson() { } //datatable转成json public static string datatabletojson(string jsonname, datatable dt) { stringbuilder json = new stringbuilder(); 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++) { json.append("\"" + dt.columns[j].columnname.tostring() + "\":\"" + dt.rows[i][j].tostring() + "\""); if (j < dt.columns.count - 1) { json.append(","); } } json.append("}"); if (i < dt.rows.count - 1) { json.append(","); } } } json.append("]}"); return json.tostring(); } //list转成json public static string objecttojson<t>(string jsonname, ilist<t> il) { stringbuilder json = new stringbuilder(); json.append("{\"" + jsonname + "\":["); if (il.count > 0) { for (int i = 0; i < il.count; i++) { t obj = activator.createinstance<t>(); type type = obj.gettype(); propertyinfo[] pis = type.getproperties(); json.append("{"); for (int j = 0; j < pis.length; j++) { json.append("\"" + pis[j].name.tostring() + "\":\"" + pis[j].getvalue(il[i], null) + "\""); if (j < pis.length - 1) { json.append(","); } } json.append("}"); if (i < il.count - 1) { json.append(","); } } } json.append("]}"); return json.tostring(); } }
2. 各种数据集转换为json
/// <summary> /// 对象转换为json字符串 /// </summary> /// <param name="jsonobject">对象</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 + ","; } return json.deletelast(jsonstring) + "}"; } /// <summary> /// 对象集合转换json /// </summary> /// <param name="array">集合对象</param> /// <returns>json字符串</returns> public static string tojson(ienumerable array) { string jsonstring = "["; foreach (object item in array) { jsonstring += json.tojson(item) + ","; } return json.deletelast(jsonstring) + "]"; } /// <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()) + ","; } return json.deletelast(jsonstring) + "]"; } /// <summary> /// 删除结尾字符 /// </summary> /// <param name="str">需要删除的字符</param> /// <returns>完成后的字符串</returns> private static string deletelast(string str) { if (str.length > 1) { return str.substring(0, str.length - 1); } return str; } /// <summary> /// datatable转换为json /// </summary> /// <param name="table">datatable对象</param> /// <returns>json字符串</returns> public static string tojson(datatable table) { string jsonstring = "["; datarowcollection drc = table.rows; for (int i = 0; i < drc.count; i++) { jsonstring += "{"; foreach (datacolumn column in table.columns) { jsonstring += "\"" + tojson(column.columnname) + "\":"; if (column.datatype == typeof(datetime) || column.datatype == typeof(string)) { jsonstring += "\"" + tojson(drc[i][column.columnname].tostring()) + "\","; } else { jsonstring += tojson(drc[i][column.columnname].tostring()) + ","; } } jsonstring = deletelast(jsonstring) + "},"; } return deletelast(jsonstring) + "]"; } /// <summary> /// datareader转换为json /// </summary> /// <param name="datareader">datareader对象</param> /// <returns>json字符串</returns> public static string tojson(dbdatareader datareader) { string jsonstring = "["; while (datareader.read()) { jsonstring += "{"; for (int i = 0; i < datareader.fieldcount; i++) { jsonstring += "\"" + tojson(datareader.getname(i)) + "\":"; if (datareader.getfieldtype(i) == typeof(datetime) || datareader.getfieldtype(i) == typeof(string)) { jsonstring += "\"" + tojson(datareader[i].tostring()) + "\","; } else { jsonstring += tojson(datareader[i].tostring()) + ","; } } jsonstring = deletelast(jsonstring) + "}"; } datareader.close(); return deletelast(jsonstring) + "]"; } /// <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 += "\"" + tojson(table.tablename) + "\":" + tojson(table) + ","; } return jsonstring = deletelast(jsonstring) + "}"; } /// <summary> /// string转换为json /// </summary> /// <param name="value">string对象</param> /// <returns>json字符串</returns> public static string tojson(string value) { if (string.isnullorempty(value)) { return string.empty; } string temstr; temstr = value; temstr = temstr.replace("{", "{").replace("}", "}").replace(":", ":").replace(",", ",").replace("[", "【").replace("]", "】").replace(";", ";").replace("\n", "<br/>").replace("\r", ""); temstr = temstr.replace("\t", " "); temstr = temstr.replace("'", "\'"); temstr = temstr.replace(@"\", @"\\"); temstr = temstr.replace("\"", "\"\""); return temstr; }
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。
上一篇: C#根据日期计算星期几的实例代码