2016-05-12 140 views
4

我有以下json代碼,有任何庫和方法,我使用mydatatable值轉換成json,因爲我發佈數據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(); 
        } 
       } 
+0

你可能需要邏輯來轉換你的數據表中的對象數組,然後你可以轉換成json字符串 –

回答

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(); 
    } 
    
+0

確實,不要自己構造json!使用序列化! – RvdK

+0

如何添加數據這種類的公共類bookinginfo {public ToLocation to_location {get;組; }} public class ToLocation { public double latitude {get;組; } public double longitude {get;組; } public Address2 address {get;組; } public object comment {get;組; } public object airport {get;組; } } –

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; 
    } 
}