【问题标题】:Converting Dynamic to Datatable将动态转换为数据表
【发布时间】:2016-08-26 08:06:49
【问题描述】:

如何将包含 json 数据结果的动态转换为数据表?

我已尽我所能,但无法将其变成数据表。

下面是我的代码和动态的 jsondata 结果。非常感谢。

 public class Response
    {
        public string ID { get; set; }
        public string HostIp { get; set; }
        public string HostMAC { get; set; }
        public string HostType { get; set; }
        public string ConnectedNetworkDeviceId { get; set; }
        public string ConnectedNetworkDeviceIpAddress { get; set; }
        public string ConnectedAPMacAddress { get; set; }
        public string ConnectedAPName { get; set; }
        public string VlanId { get; set; }
        public string LastUpdated { get; set; }
        public string AvgUpdateFrequency { get; set; }
        public string Source { get; set; }
        public string PointOfPresence { get; set; }
        public string PointOfAttachment { get; set; }
        public string ConnectedInterfaceId { get; set; }
        public string ConnectedInterfaceName { get; set; }
    }

    public class RootObject
    {
        public List<Response> Response { get; set; }
        public string Version { get; set; }
    }


IRestResponse response2 = client.Execute(request);
dynamic hostdata = JsonConvert.DeserializeObject<dynamic>(response2.Content);

{{
  "response": [
    {
      "id": "4c60d6a7-4812-40d6-a337-773af2625e56",
      "hostIp": "1.1.1.1",
      "hostMac": "00:24:d7:43:59:d8",
      "hostType": "wireless",
      "connectedNetworkDeviceId": "17184480-2617-42c3-b267-4fade5f794a9",
      "connectedNetworkDeviceIpAddress": "55.1.1.3",
      "connectedAPMacAddress": "68:bc:0c:63:4a:b0",
      "connectedAPName": "AP7081.059f.19ca",
      "vlanId": "600",
      "lastUpdated": "1467837609856",
      "avgUpdateFrequency": "1800",
      "source": "300",
      "pointOfPresence": "5a3bdb62-5def-40a1-be98-944ba2a7d863",
      "pointOfAttachment": "5a3bdb62-5def-40a1-be98-944ba2a7d863"
    },
    {
      "id": "3ef5a7c3-7f74-4e57-a5cb-1448fbda5078",
      "hostIp": "1.1.1.2",
      "hostMac": "5c:f9:dd:52:07:78",
      "hostType": "wired",
      "connectedNetworkDeviceId": "3ab14801-5c88-477d-bbb5-0aca5e5ba840",
      "connectedNetworkDeviceIpAddress": "207.1.10.1",
      "connectedInterfaceId": "1db9891e-9cee-4012-ba52-16df9db75a4a",
      "connectedInterfaceName": "GigabitEthernet1/0/47",
      "vlanId": "200",
      "lastUpdated": "1467837601200",
      "source": "200"
    },
    {
      "id": "12f9c920-24fa-4d32-bf39-4c63813aecd8",
      "hostIp": "1.1.1.3",
      "hostMac": "e8:9a:8f:7a:22:99",
      "hostType": "wired",
      "connectedNetworkDeviceId": "24ac6aa8-7759-44d5-90a3-00c83e96583d",
      "connectedNetworkDeviceIpAddress": "212.1.10.1",
      "connectedInterfaceId": "81373176-c263-4284-9970-1cb5d72414fa",
      "connectedInterfaceName": "GigabitEthernet1/0/47",
      "vlanId": "200",
      "lastUpdated": "1467836331564",
      "source": "200"
    }
  ],
  "version": "1.0"
}}

【问题讨论】:

  • 如上代码所示,JSON 响应是 Response 类型(类类型),那么为什么不能从列表类型转换为数据表。每次点击服务都会有不同的结果吗?我们可能需要更多信息,为什么您要尝试将类型转换为动态类型而不是特定类型。
  • 另外,我对你的命名风格有意见。通常,所有属性都必须具有 Pascal 大小写 - 即 ID、HostID、HostMAC、HostType。虽然它可能是您展示的示例,也可能是您的风格,但如果您遵循这些风格会很好。

标签: c# json datatable


【解决方案1】:

首先将 JSON 转换为自定义 C# 对象,然后查看以下链接了解如何将 JSON 转换为特定类类型, https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object

一旦你有了一个列表对象,然后使用下面的代码 sn-p 将 json 转换为数据表。

    /// <summary>
    /// Convert a list to a DataTable
    /// </summary>
    /// <typeparam name="T">List of type T</typeparam>
    /// <param name="data">list</param>
    /// <returns>Returns DataTable</returns>
    public static DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
        {
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        }

        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            }

            table.Rows.Add(row);
        }

        return table;
    }

【讨论】:

  • 试一试,如果有任何帮助,请告诉我们。
【解决方案2】:

您可以将 json 反序列化为 RootObject 类型。

像这样:

var result = JsonConvert.DeserializeObject<RootObject>(response2.Content);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2019-06-07
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多