【问题标题】:Complex Nested JSON Array Conversion to DataTable - C#复杂的嵌套 JSON 数组转换为 DataTable - C#
【发布时间】:2019-03-06 20:54:36
【问题描述】:

我对 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 字段。 我承认我的问题是对另一个重复问题的延伸

【问题讨论】:

  • 为什么您尝试的解决方案不成功?什么不起作用?
  • 我尝试的一些解决方案没有包括所有列,我认为这可能是由于列标题更相似(例如 to/number 和 from/number 可能有冲突,所以我只是得到 to/number 列)。我尝试过的其他东西不断返回反序列化异常消息,对于我的生活,我无法破解(例如 Newtonsoft.Json.JsonSerializationException:'在完成反序列化对象后在 JSON 字符串中找到附加文本。路径'[0 ].uuid',第 3 行,第 11 位。')
  • Convert JSON to DataTable的可能重复

标签: c# json datatable


【解决方案1】:

好的,这里是将您的 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 序列化为创建的模型,然后创建您的 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;
    }
}

你的 DataTable 看起来像这样:

编辑说明: 我创建了一个模型,只是为了将您需要的数据显示到 DataTable 中。由于我已将 JSON 数据解析到我的 C# 列表中,因此我可以使用 foreach 循环访问代码中显示的数据。然后我只需获取我需要的数据,然后创建我的数据表。

干杯。

【讨论】:

  • 感谢您的帮助,但似乎仍然存在小问题。我仍然没有得到关于 JSON 的“从”和“到”部分的详细信息。然而,我得到了名为“from”和“to”的列,这比我以前更远。
  • 我不太明白你的意思?在您的模型中,To、From 和 AnsweredBy 是在反序列化过程中解析数据的对象。您需要处理他们的数据并在您的 DataTable 中显示。
  • 您在编辑中拥有的那个 DataTable 正是我得到的。我需要的是 DataTable 包含有关 from/number 和 answers_by/number 的信息。我认为这个网站将最好地展示我的意图konklone.io/json。你说我需要在我的数据表中处理我的数据和显示。我不太清楚你的意思是什么
  • 请检查更新的答案以符合您的要求。请让我知道它是否足以满足您的需要。谢谢
  • 太完美了!这正是我所需要的。为丰富的 C# 知识道歉,这绝对是我自己的学习曲线
猜你喜欢
  • 2022-12-31
  • 1970-01-01
  • 2020-09-29
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多