1.序列化啊 宝贝!!
public string DataTableToJsonWithJsonNet(DataTable table) { string jsonString=string.Empty; jsonString = JsonConvert.SerializeObject(table); return jsonString; }
额
2.还有一个恶心点的方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public string DataTableToJson(DataTable table) { var JsonString = new StringBuilder(); if (table.Rows.Count > 0) { JsonString.Append("["); for (int i = 0; i < table.Rows.Count; i++) { JsonString.Append("{"); for (int j = 0; j < table.Columns.Count; j++) { if (j < table.Columns.Count - 1) { JsonString.Append(""" + table.Columns[j].ColumnName.ToString() + "":" + """ + table.Rows[i][j].ToString() + "","); } else if (j == table.Columns.Count - 1) { JsonString.Append(""" + table.Columns[j].ColumnName.ToString() + "":" + """ + table.Rows[i][j].ToString() + """); } } if (i == table.Rows.Count - 1) { JsonString.Append("}"); } else { JsonString.Append("},"); } } JsonString.Append("]"); } return JsonString.ToString(); }