如何 DataTable格式转换json格式
程序员文章站
2022-03-20 09:36:17
1.序列化啊 宝贝!! public string DataTableToJsonWithJsonNet(DataTable table) { string jsonString=string.Empty; jsonString = JsonConvert.SerializeObject(table ......
1.序列化啊 宝贝!!
public string datatabletojsonwithjsonnet(datatable table)
{
string jsonstring=string.empty;
jsonstring = jsonconvert.serializeobject(table);
return jsonstring;
}
额
2.还有一个恶心点的方法
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(); }