如何将数据表转换为json

时间:2016-05-12 11:11:35

标签: c# winforms

我有以下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();
                   }
               }

2 个答案:

答案 0 :(得分:3)

按照以下步骤以非常有效和更好的方式完成此事:

  1. 创建一个具有要求发送的必需属性的类。

    //for example
    public class Request
    {
      public List<Vehicle> vehicles { get; set; }
    }
    
    public class Vehicle
    {
       public string vehicle_type {get; set;}
    }
    
  2. 将值分配给对象

    Request request =new Request();
    request.vehicles  = new List<Vehicle>();
    // and so on
    
  3. 使用Newtonsoft.Json将对象序列化为:

    var json = JsonConvert.SerializeObject(request);
    
  4. 使用json

    调用您的请求
    using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", json))
    {
        string responseData = await response.Content.ReadAsStringAsync();
    }
    

答案 1 :(得分:1)

这有助于在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;
    }
}