如何将包含json数据结果的动态转换为数据表?
我已尽力而为但却无法将其变成数据表。
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"
}}
答案 0 :(得分:1)
您可以将json反序列化为RootObject类型。
像这样:
var result = JsonConvert.DeserializeObject<RootObject>(response2.Content);
答案 1 :(得分:0)
首先将JSON转换为自定义C#对象,并参阅下面有关如何将JSON转换为特定类类型的链接, https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object
一旦你有了一个列表对象,然后使用下面的代码片段将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;
}