c# - 嵌套的 Json 字符串到 DataTable

标签 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

最佳答案

您需要调整您的 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);

关于c# - 嵌套的 Json 字符串到 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503573/

相关文章:

c# - 使用 Linq Select 将实体映射到 DTO 的最简洁方法?

c# - 了解 C++ DLL 使用 union 映射到 C#

c# - 如何在不显式调用方法的情况下向引用的程序集提供值?

android - Gson:反序列化可以是单个对象或数组的对象

c# - 如何获取字段的自定义属性值?

php - 如何查找包含 JSON 数据的子字符串?

c# - 将 DataGrid 绑定(bind)到具有自定义对象类型列的 DataTable

C# 从 LinQ XDocument 加载数据表

r - 仅保留最近的日期

mysql - 如何将 mysql 查询 "group by statement"结果转换为 json 结构?