1 回答
TA贡献1993条经验 获得超5个赞
好的,这里是将您的 JSON 结构解析为 C# 列表的代码。获得此列表后,您可以使用您研究过的方法将其转换为 DataTable。我已经根据您的 JSON 结构创建了一个示例数据表。
您的模型将是:
public class JsonInfo
{
public string uuid { get; set; }
public string call_start_time { get; set; }
public string call_duration { get; set; }
public string created_on { get; set; }
public string cost { get; set; }
public string call_type { get; set; }
public string answered { get; set; }
public string has_recording { get; set; }
public string call_route { get; set; }
public string is_fax { get; set; }
public From from { get; set; }
public To to { get; set; }
public AnsweredBy answered_by { get; set; }
}
public class From
{
public string uuid { get; set; }
public string type { get; set; }
public string name { get; set; }
public string nickname { get; set; }
public string number { get; set; }
}
public class To
{
public string uuid { get; set; }
public string type { get; set; }
public string name { get; set; }
public string nickname { get; set; }
public string number { get; set; }
}
public class AnsweredBy
{
public string uuid { get; set; }
public string type { get; set; }
public string name { get; set; }
public string nickname { get; set; }
public string number { get; set; }
}
//Create a model to show your information
public class DisplayJsonInfo
{
public string uuid { get; set; }
public string call_start_time { get; set; }
public string call_duration { get; set; }
public string created_on { get; set; }
public string cost { get; set; }
public string from_uuid { get; set; }
public string from_type { get; set; }
public string from_name { get; set; }
public string from_nickname { get; set; }
}
要将您的 JSON 序列化为创建的模型,然后创建您的数据表:
var json = "[{\"uuid\":\"af9fcfc7-61af-4484-aaa8-7dhcced2f2f79\",\"call_start_time\":1551892096171,\"call_duration\":1150,\"created_on\":\"2019-03-06\",\"cost\":0,\"call_type\":\"inbound\",\"from\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 7*** ******\"},\"to\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 **** ******0\"},\"answered\":true,\"answered_by\":{\"uuid\":\"48bj949-e72e-4239-a337-e181a1b45841\",\"type\":\"sipuser\",\"name\":\"SipUser\",\"nickname\":\"Myself\",\"number\":\"1001\"},\"has_recording\":true,\"call_route\":\"c30e45g0e-3da4-4a67-9a04-27e1d9d31129\",\"is_fax\":false},{\"uuid\":\"f62kmop2b-f929-4afc-8c05-a8c1bc43225d\",\"call_start_time\":1551890795202,\"call_duration\":12,\"created_on\":\"2019-03-06\",\"cost\":0.012,\"call_type\":\"outbound\",\"from\":{\"uuid\":\"68a50328-f5b0-4c5e-837c-667ea50878f3\",\"type\":\"sipuser\",\"name\":\"Spare\",\"nickname\":\"Spare\",\"number\":\"1011\"},\"to\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 *** *** ****\"},\"answered\":true,\"answered_by\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 ***1*****0\"},\"has_recording\":false,\"call_route\":\"\",\"is_fax\":false},{\"uuid\":\"b1b495c4-ecf6-44c0-8020-28c9eddc7afe\",\"call_start_time\":1551890780607,\"call_duration\":10,\"created_on\":\"2019-03-06\",\"cost\":0.012,\"call_type\":\"outbound\",\"from\":{\"uuid\":\"68a50328-f5b0-4c5e-837c-667ea50878f3\",\"type\":\"sipuser\",\"name\":\"Spare\",\"nickname\":\"Spare\",\"number\":\"1011\"},\"to\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 *** *** ****\"},\"answered\":true,\"answered_by\":{\"uuid\":\"\",\"type\":\"number\",\"name\":\"\",\"nickname\":\"\",\"number\":\"+44 *** *** ****\"},\"has_recording\":false,\"call_route\":\"\",\"is_fax\":false}]";
var data = JsonConvert.DeserializeObject<List<JsonInfo>>(json);
//Call your helper method to convert
CreateDataTable cl = new CreateDataTable();
DataTable dt = new DataTable();
DisplayJsonInfo show = new DisplayJsonInfo();
List<DisplayJsonInfo> showInfo = new List<DisplayJsonInfo>();
foreach(var value in data)
{
showInfo.Add(new DisplayJsonInfo
{
call_duration = value.call_duration,
call_start_time = value.call_start_time,
cost = value.cost,
uuid = value.uuid,
created_on = value.created_on,
from_uuid = value.from.uuid,
from_name = value.from.name,
from_type = value.from.type,
from_nickname=value.from.nickname
});
}
dt = cl.ToDataTable(showInfo);
一旦你有了这个使用像 ToDataTable() 这样的辅助扩展,这里提到:Convert JSON to DataTable
编辑:
因此,在解析出信息并使用此助手将您的 List 转换为 DataTable 之后:
using System;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
public class CreateDataTable
{
public 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 看起来像这样:
编辑说明: 我创建了一个模型,仅用于将您所需的数据显示到 DataTable 中。由于我已将 JSON 数据解析到我的 C# 列表中,因此我可以使用 foreach 循环访问代码中显示的数据。然后我只需获取所需的数据,然后创建我的数据表。
- 1 回答
- 0 关注
- 315 浏览
添加回答
举报