【发布时间】:2018-02-10 15:01:54
【问题描述】:
我需要将下面的 Json 字符串转换为 DataTable。
Json 示例:
[
{
'extension': '0001',
'name': 'User 1',
'email': 'user1@mail.se',
'mobile': '+46000000',
'profile': {
'available': false,
'name': 'Gone for the day',
'until': '2017-09-01 08:00:00',
'message': 'Do not call me'
},
'calls': [],
},
{
'extension': '0002',
'name': 'User 2',
'email': 'user2@mail.se',
'mobile': '+46000000',
'profile': {
'available': false,
'name': 'Gone for the day',
'until': '2017-09-01 08:00:00',
'message': 'Do not call me'
},
'calls': [],
}
]
我可以阅读“个人资料”之前的所有内容:,我如何访问个人资料下的下一级数据? (profile.avialable,profile.name, profile.until, profile.message) 并将其添加到数据表?
这是我的课
public class Profile
{
public bool available { get; set; }
public string name { get; set; }
public string until { get; set; }
public object message { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string extension { get; set; }
public string mobile { get; set; }
public Profile profile { get; set; }
}
这是我的代码
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Defining type of data column gives proper data table
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
PopulateList()
{
var data = JsonConvert.DeserializeObject<List<RootObject>>(JsonData);
DataTable dt = ToDataTable(data);
GridView1.DataSource = dt;
GridView1.DataBind();
}
JsonData 包含我的 JSON 的位置
有人知道吗?
【问题讨论】:
-
实际问题是什么?你说“如何进入下一个级别”是什么意思?你想在哪里访问它?
-
如果实际问题是嵌套类转换为 DataTable(与反序列化和 json 无关!)那么你真的需要 DataTable 吗?你不能使用 List/BindingList 吗?是的,有很多问题,因为你的问题很模糊。
-
你说得对,问题可能很模糊,json 来自电话交换机的 API,目标是创建一个包含联系人和个人资料信息的动态电话列表。它需要是一个包含名称、扩展名、移动、profile.avialable、profile.name、profile.until 列的表