20160605
简单的DaTable转Json
private string DtConvertJson(DataTable dt , string modelName="") { int colNum = dt.Columns.Count; int rowNum = dt.Rows.Count; StringBuilder sb = new StringBuilder(); if(rowNum > 1) sb.Append("{""+ modelName +"":" + "["); for (int i = 0; i < rowNum; i++) { sb.Append("{"); for(int k = 0; k < colNum; k++) { sb.Append(""" + dt.Columns[k].ColumnName + "":"" + dt.Rows[i][k] +"""); if (k < colNum - 1) sb.Append(","); } sb.Append("}"); if (i < rowNum - 1) sb.Append(","); }; if (rowNum > 1) sb.Append("]}"); return sb.ToString(); }
如果只有1行数据格式就是:{key:value,key:value};
1行以上就是{object:[{key:values,key:value}]}
以后扩展Json转obj,转Model