嵌套的Json String到DataTable

时间:2016-07-21 11:53:47

标签: c# json datatable json.net

我需要将以下Json字符串转换为DataTable。

{  
   "pnr":"1234567890",
   "train_num":"12311",
   "train_name":"HWH DLIKLK MAI",
   "doj":"23-12-2013",
   "from_station":
   {  
      "code":"DLI",
      "name":"Delhi"
   },
   "to_station":
   {  
      "code":"KLK",
      "name":"Kalka"
   }
}

在DataTable中我需要显示

train_num
train_name
doj
from_station(name only)
to_station(name only)

我现在拥有的是,

public class Train
{
public string train_num { get; set; }
public string train_name { get; set; }
public string doj { get; set; }
public from_station from_station { get; set; }
public to_station to_station { get; set; }
}

public class from_station
{
public string code { get; set; }
public string name { get; set; }
}
public class to_station
{
public string code { get; set; }
public string name { get; set; }
}

public static DataTable ToDataTable(Train data)
{
    PropertyDescriptorCollection props =
    TypeDescriptor.GetProperties(typeof(Train));
    DataTable table = new DataTable();

    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];

        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(data);
        }
        table.Rows.Add(values);
    return table;
}

var data = JsonConvert.DeserializeObject<Train>(JsonString);
    dt = ToDataTable(data);
    ui_grdVw_EmployeeDetail1.DataSource = dt;
    ui_grdVw_EmployeeDetail1.DataBind();

我只在数据表

中获得三列
train_num
train_name
doj

2 个答案:

答案 0 :(得分:4)

您需要将DataTable转换方法调整为更通用。然后传递你希望它成形的数据。

public static DataTable ToDataTable<T>( IList<T> data)
{
    PropertyDescriptorCollection props =
        TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];
    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }
        table.Rows.Add(values);
    }
    return table;
}

注意:以下方法可用于将任何List转换为DataTable。

用法:

var data = JsonConvert.DeserializeObject<Train>(JsonString);


var shapedData = Enumerable.Range(0, 1).Select(x =>
                    new
                    {
                        train_num = data.train_num,
                        train_name = data.train_name,
                        doj = data.doj,
                        from_station = data.from_station.name,
                        to_station = data.to_station.name
                    }).ToList();

DataTable dt = ToDataTable(shapedData);

答案 1 :(得分:0)

尝试此代码。

    public DataTable jsonToDataTable(string jsonString)
    {
        var jsonLinq = JObject.Parse(jsonString);

        // Find the first array using Linq
        var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
        var trgArray = new JArray();
        foreach (JObject row in srcArray.Children<JObject>())
        {
            var cleanRow = new JObject();
            foreach (JProperty column in row.Properties())
            {
                // Only include JValue types
                if (column.Value is JValue)
                {
                    cleanRow.Add(column.Name, column.Value);
                }
            }
            trgArray.Add(cleanRow);
        }

        return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
    }