C# 如何格式化put请求的数据行内容
我需要以以下格式将数据表中的数据流作为一系列记录发送:C# 如何格式化put请求的数据行内容,c#,http,C#,Http,我需要以以下格式将数据表中的数据流作为一系列记录发送: "{""firstName"":""Billy Lee"",""lastName"":""Presley"", ... }" 有没有一种简单的方法将datatable或datarow的内容转换成这种格式 谢谢公共字符串DataRowToJSON(System.Data.DataRow) { System.Collections.Generic.List字段=新的System.Collections.Generic.List(); fore
"{""firstName"":""Billy Lee"",""lastName"":""Presley"", ... }"
有没有一种简单的方法将datatable或datarow的内容转换成这种格式
谢谢公共字符串DataRowToJSON(System.Data.DataRow)
{
System.Collections.Generic.List字段=新的System.Collections.Generic.List();
foreach(System.Data.DataColumn项目在row.Table.Columns中)字段。添加(string.Format(“\“{0}\”:\“{1}\”,item.ColumnName,row[item].ToString());
返回string.Format(“{{0}}”,string.Join(“,”,Fields));
}
我不完全理解您需要在结果字符串中引用多少引号…谢谢,我将在上午(下午5点后)尝试使用它
public string DataRowToJSON(System.Data.DataRow row)
{
System.Collections.Generic.List<string> Fields = new System.Collections.Generic.List<string>();
foreach (System.Data.DataColumn item in row.Table.Columns) Fields.Add(string.Format("\"{0}\":\"{1}\"", item.ColumnName, row[item].ToString()));
return string.Format("{{{0}}}", string.Join(",", Fields));
}