[英]how to datatable convert into json
我有以下json代码,有任何库和方法,我使用mydatatable值转换为json因为我发布数据json格式。 很难添加这些类型的参数。
using (var content = new StringContent("{ \"vehicles\": [ { \"vehicle_type\": \""+ vehicale_type +"\", \"vehicle_id\": \"" +vehicle_id+"\"," +
"\"vehicle_plate\": \"" +vehicle_plate+ "\", \"latitude\": "+latitude +"+, \"longitude\": "+longitude+", \"eta_minutes\": null, " +
"\"make\": \"null\", \"model\": \""+carModel+"\", \"color\": \"Black\", \"status\": \""+status+"\", " +
"\"driver_id\": \"" + driver_id + "\", \"driver_phone\": \"" + driver_phone + "\", \"driver_first_name\": \"" + driver_first_name + "\", " +
"\"driver_last_name\": \"" + driver_last_name + "\", \"direction\": { \"kph\": 20, \"heading\": 90 } }, " +
"{ \"vehicle_type\": \"" + vehicale_type2 + "\", \"vehicle_id\": \""+vehicle_id2+"\", \"vehicle_plate\": \""+vehicle_plate2+"\", \"latitude\":"+latitude2+", " +
"\"longitude\":" + longitude2 + ", \"eta_minutes\": null, \"make\": \"null\", \"model\": \"" + carModel2+ "\", " +
"\"color\": \"Black\", \"status\": \""+status2+"\", \"driver_id\": \""+driver_id2+"\", \"driver_phone\": \""+driver_phone2+"\", " +
"\"driver_first_name\": \"" + driver_first_name2+ "\", \"driver_last_name\": \"" + driver_last_name2 + "\", \"direction\": { \"kph\": 20, " +
"\"heading\": 90 } } ]}", System.Text.Encoding.Default, "application/json"))
{
using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", content))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
按照以下步骤以非常有效和更好的方式完成此任务:
创建一个具有要求发送的必需属性的类。
//for example public class Request { public List<Vehicle> vehicles { get; set; } } public class Vehicle { public string vehicle_type {get; set;} }
将值分配给对象
Request request =new Request(); request.vehicles = new List<Vehicle>(); // and so on
使用Newtonsoft.Json
将对象序列化为:
var json = JsonConvert.SerializeObject(request);
用json调用你的请求
using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", json)) { string responseData = await response.Content.ReadAsStringAsync(); }
这将有助于在json字符串中格式化您的数据表值。 传递你的实际数据表对象和方法将返回json的字符串。
public string DataTableToJsonObj(DataTable dt)
{
DataSet ds = new DataSet();
ds.Merge(dt);
StringBuilder JsonString = new StringBuilder();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
JsonString.Append("[");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
JsonString.Append("{");
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
if (j < ds.Tables[0].Columns.Count - 1)
{
JsonString.Append("\"" + ds.Tables[0].Columns[j].ColumnName.ToString() + "\":" + "\"" + ds.Tables[0].Rows[i][j].ToString() + "\",");
}
else if (j == ds.Tables[0].Columns.Count - 1)
{
JsonString.Append("\"" + ds.Tables[0].Columns[j].ColumnName.ToString() + "\":" + "\"" + ds.Tables[0].Rows[i][j].ToString() + "\"");
}
}
if (i == ds.Tables[0].Rows.Count - 1)
{
JsonString.Append("}");
}
else
{
JsonString.Append("},");
}
}
JsonString.Append("]");
return JsonString.ToString();
}
else
{
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.