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

时间:2019-03-06 20:54:37

标签: c# json datatable

我对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"
    },
    "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
  }
]

我希望它呈现的方式必须类似于该网站呈现数据表的方式

https://konklone.io/json/

我到处都是网络,开始用尽所有选项。我确实尝试过使用类来创建它,但是没有成功。 我还尝试了以下所有示例(以及其他示例)

https://www.code-sample.com/2017/04/convert-json-to-datatable-asp-net-c.html

Import Complex JSON file to C# dataTable

Convert JSON to DataTable

https://www.codeproject.com/Questions/590838/convertplusJSONplusstringplustoplusdatatable

即使将其放入数据集,然后我也会从那里整理表。任何帮助将不胜感激。

编辑

我将解释为什么这与此处假定的重复问题有点不同

Convert JSON to DataTable

这个问题的答案似乎没有考虑到我已经嵌套了需要访问的JSON。我已经尝试过了,但仍然没有任何from / number字段和to / number字段。 我承认我的问题是对其他重复问题的延伸

1 个答案:

答案 0 :(得分:0)

好的,这是将您的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序列化为创建的Model,然后创建您的DataTable:

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

您的数据表如下所示:

enter image description here

修改说明: 我创建的模型仅用于将所需的数据显示到DataTable中。由于已将JSON数据解析到C#列表中,因此可以使用foreach循环访问代码中显示的数据。然后,我只需获取所需的数据,然后创建我的数据表即可。

干杯。

相关问题