如何 DataTable格式轉換json格式

1.序列化啊  寶貝!!json

public string DataTableToJsonWithJsonNet(DataTable table)
{
   string jsonString=string.Empty;
   jsonString = JsonConvert.SerializeObject(table);
   return jsonString;
}

  

額  ide

 

2.還有一個噁心點的方法ui

 

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(); }
自定義格式化
相關文章
相關標籤/搜索