DataSet转Json 方法

防止忘记,留着备用   



/// <summary> /// DATASET转JSON /// </summary> /// <param name="ds"></param> /// <returns></returns> public static string ToJson(DataSet ds) { try { string json = "["; foreach (DataRow i in ds.Tables[0].Rows) { json += "{"; foreach (DataColumn column in ds.Tables[0].Columns) { json += "'" + column.ColumnName + "':"; //json += "'" + column.ColumnName + "':'" + i[column.ColumnName].ToString() + "',"; if (column.DataType == typeof(DateTime) || column.DataType == typeof(string)) { json += "'" + i[column.ColumnName].ToString() + "',"; } else { json += "" + i[column.ColumnName].ToString() + ","; } } json = json.Substring(0, json.LastIndexOf(",")) + "},"; } json = json.Substring(0, json.LastIndexOf(",")); return json + "]"; } catch { return "没有数据"; } }

 

posted @ 2015-04-24 15:03  呆萌的小黑  阅读(565)  评论(0编辑  收藏  举报