嵌套的Json字符串到DataTable [英] Nested Json String to DataTable

查看:137
本文介绍了嵌套的Json字符串到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Need to convert the following Json string to 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中,我需要显示

In DataTable I need to Display

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();

我在datatable中只收到三列

I am getting only three columns in datatable

train_num
train_name
doj


推荐答案

你需要调整你的DataTable转换方法是更通用的。然后传递给你想要的数据。

You need to tweak your DataTable conversion method to be more Generic. Then pass for it the data shaped as you want it to be.

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

注意:可以使用以下方法将任何列表转换为DataTable。

Note: the below method can be used to convert any List to 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);

这篇关于嵌套的Json字符串到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆