复杂的嵌套 JSON 数组转换为 DataTable - C#

我对 JSON 完全陌生,需要能够将我的 JSON 字符串转换为 DataTable。


这是我的 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"

    },


隔江千里
浏览 275回答 1
1回答

ibeautiful

好的,这里是将您的 JSON 结构解析为 C# 列表的代码。获得此列表后,您可以使用您研究过的方法将其转换为 DataTable。我已经根据您的 JSON 结构创建了一个示例数据表。您的模型将是:public class JsonInfo{&nbsp; &nbsp; public string uuid { get; set; }&nbsp; &nbsp; public string call_start_time { get; set; }&nbsp; &nbsp; public string call_duration { get; set; }&nbsp; &nbsp; public string created_on { get; set; }&nbsp; &nbsp; public string cost { get; set; }&nbsp; &nbsp; public string call_type { get; set; }&nbsp; &nbsp; public string answered { get; set; }&nbsp; &nbsp; public string has_recording { get; set; }&nbsp; &nbsp; public string call_route { get; set; }&nbsp; &nbsp; public string is_fax { get; set; }&nbsp; &nbsp; public From from { get; set; }&nbsp; &nbsp; public To to { get; set; }&nbsp; &nbsp; public AnsweredBy answered_by { get; set; }}public class From{&nbsp; &nbsp; public string uuid { get; set; }&nbsp; &nbsp; public string type { get; set; }&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public string nickname { get; set; }&nbsp; &nbsp; public string number { get; set; }}public class To{&nbsp; &nbsp; public string uuid { get; set; }&nbsp; &nbsp; public string type { get; set; }&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public string nickname { get; set; }&nbsp; &nbsp; public string number { get; set; }}public class AnsweredBy{&nbsp; &nbsp; public string uuid { get; set; }&nbsp; &nbsp; public string type { get; set; }&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public string nickname { get; set; }&nbsp; &nbsp; public string number { get; set; }}//Create a model to show your informationpublic class&nbsp; DisplayJsonInfo&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string uuid { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string call_start_time { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string call_duration { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string created_on { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string cost { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string from_uuid { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string from_type { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string from_name { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string from_nickname { get; set; }&nbsp; &nbsp; }要将您的 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 convertCreateDataTable cl = new CreateDataTable();DataTable dt = new DataTable();DisplayJsonInfo show = new DisplayJsonInfo();List<DisplayJsonInfo> showInfo = new List<DisplayJsonInfo>();&nbsp; &nbsp; &nbsp; &nbsp; foreach(var value in data)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showInfo.Add(new DisplayJsonInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; call_duration = value.call_duration,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; call_start_time = value.call_start_time,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cost = value.cost,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uuid = value.uuid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; created_on = value.created_on,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from_uuid = value.from.uuid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from_name = value.from.name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from_type = value.from.type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from_nickname=value.from.nickname&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; 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)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; PropertyDescriptorCollection props =&nbsp; &nbsp; &nbsp; &nbsp; TypeDescriptor.GetProperties(typeof(T));&nbsp; &nbsp; &nbsp; &nbsp; DataTable table = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < props.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyDescriptor prop = props[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add(prop.Name, prop.PropertyType);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; object[] values = new object[props.Count];&nbsp; &nbsp; &nbsp; &nbsp; foreach (T item in data)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < values.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values[i] = props[i].GetValue(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(values);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return table;&nbsp; &nbsp; }}您的 DataTable 看起来像这样:编辑说明:&nbsp;我创建了一个模型,仅用于将您所需的数据显示到 DataTable 中。由于我已将 JSON 数据解析到我的 C# 列表中,因此我可以使用 foreach 循环访问代码中显示的数据。然后我只需获取所需的数据,然后创建我的数据表。
打开App,查看更多内容
随时随地看视频慕课网APP