将对象/动态对象作为DataGridView

时间:2018-07-02 11:48:37

标签: c# json

好吧,解密后我得到了一个JSON字符串

"{\"HardwareInformation\":{\"NumberOfProcessors\":8,\"PageSize\":4096,\"ProcessorType\":586,\"ProcessorTypeText\":\"]\",\"ActiveProcessorMask\":255},\"SystemInformation\":{\"ComputerName\":\"PD-AT-23006\",\"UserName\":\"230010\",\"SystemDirectory\":\"C:\\\\\\\\Windows\\\\\\\\system32\",\"WindowsDirectory\":\"C:\\\\\\\\Windows\",\"Is64BitWindows\":1},\"EnvironmentVariables\":{\"OS\":\"Windows_NT\",\"PATH\":\"C:\\\\\\\\Program Files (x86)\\\\\\\\NVIDIA Corporation\\\\\\\\PhysX\\\\\\\\Common;C:\\\\\\\\Program Files (x86)\\\\\\\\Intel\\\\\\\\iCLS Client\\\\\\\\;C:\\\\\\\\Program Files\\\\\\\\Intel\\\\\\\\iCLS Client\\\\\\\\;C:\\\\\\\\Windows\\\\\\\\system32;C:\\\\\\\\Windows;C:\\\\\\\\Windows\\\\\\\\System32\\\\\\\\Wbem;C:\\\\\\\\Windows\\\\\\\\System32\\\\\\\\WindowsPowerShell\\\\\\\\v1.0\\\\\\\\;C:\\\\\\\\Program Files\\\\\\\\Intel\\\\\\\\Intel(R) Management Engine Components\\\\\\\\DAL;C:\\\\\\\\Program Files\\\\\\\\Intel\\\\\\\\Intel(R) Management Engine Components\\\\\\\\IPT;C:\\\\\\\\Program Files (x86)\\\\\\\\Intel\\\\\\\\Intel(R) Management Engine Components\\\\\\\\DAL;C:\\\\\\\\Program Files (x86)\\\\\\\\Intel\\\\\\\\Intel(R) Management Engine Components\\\\\\\\IPT\",\"HOMEPATH\":\"\\\\\\\\\",\"TEMP\":\"C:\\\\\\\\Users\\\\\\\\230010\\\\\\\\AppData\\\\\\\\Local\\\\\\\\Temp\"}}\0"

格式化的JSON字符串是

{
"HardwareInformation": {
  "NumberOfProcessors": 8,
  "PageSize": 4096,
  "ProcessorType": 586,
  "ProcessorTypeText": "]",
  "ActiveProcessorMask": 255
},
"SystemInformation": {
  "ComputerName": "PD-AT-23006",
  "UserName": "230010",
  "SystemDirectory": "C:\\\\Windows\\\\system32",
  "WindowsDirectory": "C:\\\\Windows",
  "Is64BitWindows": 1
},
"EnvironmentVariables": {
  "OS": "Windows_NT",
  "PATH": "C:\\\\Program Files (x86)\\\\NVIDIACorporation\\\\PhysX\\\\Common;
..............................and more

好吧,现在我想将诸如“ HardwareInformation”,“ SystemInformation”和“ EnvironmentVariables”之类的标题信息设置为一列,并在其下一行设置其他信息。

因为总会出现一些新的列或行,所以代码独立于我工作很重要(就像不为数据添加新类一样)

我尝试使用Newtonsoft.JSON并以动态方式获取JSON

dynamic json = Crypt.getEncodedTXT("37MpQhzhSCGuReZnHZblzk+TJnD0uYkTbcoo0JvpV/bMeLFuckuVxYzbZlKOrfAqtwHOeaSFzGIRZybjgFVaz7BISjxEg5wTlZch788Bm4nMegha0rNYpaYXCliZ50MTimxxin0ch3CDIHVQTN1U7oxDgA2gt+2+5ceQnhKMpY5YOQFev44HCdpNVb03AO+HT9F0MtlUsIG84wg/Ez3i1Ab9+HV/Hs26hhbIoJEdGxh+6OP6c0/bp3NuloCmuC5rFb010KxSBxmNcFhmU59HYfDJv/W7gnfKhEaP6doQ9di5ou3GQNehy+LGGxK/2HqTayfurqtjJ9OT9TIZ0nJSH25C2VAvLPqG12WA/8qZgmbDscy6WZsW/LA5xbHN3j2bIo3s8xjMQyvDtvEoXBB7/XK3JsWkIk5xNYTCPuzkZEg8hh3jcrsEmitAhyITBtBmsNm2foyL8oMhO1fw4efj4oEYyQKSZJEoMANamKPdrIHbGpQDnm4Yn+aOVsBI2i980t5SlO9WNZ0vh4h4lPtmxLKSS6YMqu/14qpDpVgytBoK1aRFKRIxdPo/su8uO7R2m1VFgmWH4cE2Vk0kLNORi6gPmqkhunbPHZwN/qWiU2v6Lu3VjQ3RbkSayI+JzBw7IF+GDVNN5Sw3HS5uPpVGxPNLZTQ2V1xJ+zQB+A79Ps1lX1zLh8cbmgbT7CDBjhJQ6TNCU0UeLuSoCQOJVoRPyhY7ZmNINHLaJk4wboVg3D2vASr2P6/XOeOtWsjyjjD6SxW+qeUqaRvSAAJ3XlvYMTn0wbGjl/CNymLV28dUnWJHlM+HbgZjrvV7psb8I83B+O1wRBQwbMtuwBy7zsFWfErqscADwTCuksbRJuRfOQFRX/JI0FewRJtHZh6XDQ5QACOLkUUdQAo/Nhb8jhpnMQn5j5UOsO97lfVOmFp0MGovxyNLdjTefDnptForKmhkgDdcel8dpM82UUieBJG4igoSf+qRbhmihdutHB3lP7B0barxc4+t1As+wollx0EXSIBCAZtSmwHaaVVn/ZcxI36dIAoSjb5EIHO18JfWnm0Q/dwwYkUm4Xr/2thdtJeL9icmIDn+3bmo76jXu6gdUbhsUYZRBOOOsfmhL6dJMDMM5q/s0d2scaA+22XnnfiebDhet7i8xQe1DfnAME4qjlL1gBTqSPDKG/XZ2pnH5EpVbn+l32Oj2WD2YWem9TKPZoOJ6j3CwYfm0M0A1Z1FsoXtHrfXw535tOTVbn9ThrJhSMA88OaG10Huvi/A2MzeuaiisIfPwXiWZm8wa0mveR/hD1I5I7y+0/qEoavXDhwmpIytPJTIdg6gEHr7nsqoGNTKCg4ZBBMqKbIucV7PDSznus4zAt0McVN1dVt92k/pUOgmeW7SshYZhsrGrUz8Z9lDXdWNumgoX1iSIuDpCQ==");

(请忽略解密)并作为JObject

JObject json1 = JObject.Parse(json);

动态未格式化,而JObject已格式化。

dynamic debug pic

JObject debug pic

该表应如下所示: table

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

首先,将json反序列化为Dictionary<string, Dictionary<string, object>>。计算行数和列数。添加列和行。

string jstr = "{\"HardwareInformation\": {\"NumberOfProcessors\": 8,\"PageSize\": 4096,\"ProcessorType\": 586,\"ProcessorTypeText\": \"]\",\"ActiveProcessorMask\": 255},\"SystemInformation\": {\"ComputerName\": \"PD-AT-23006\",\"UserName\": \"230010\",\"SystemDirectory\": \"C:\\\\Windows\\\\system32\",\"WindowsDirectory\": \"C:\\\\Windows\",\"Is64BitWindows\": 1},\"EnvironmentVariables\": {\"OS\": \"Windows_NT\",\"PATH\": \"C:\\\\Program Files (x86)\\\\NVIDIACorporation\\\\PhysX\\\\Common\"}}";

var info = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(jstr);

DataTable dt = new DataTable();

var Maxcolumn = info.Count;
var maxRows = info.Select(x => x.Value.Count).Max();

// Add Columns
foreach (var column in info)
    dt.Columns.Add(new DataColumn(column.Key, typeof(string)));

// Add Rows
for (int r = 0; r < maxRows; r++)
{
    DataRow newrow = dt.NewRow();

    for (int c = 0; c < Maxcolumn; c++)
    {
        var columnDic = info.Values.ElementAt(c);

        var key = r < columnDic.Count ? columnDic.Keys.ElementAt(r) : "";
        var value = r < columnDic.Count ? columnDic.Values.ElementAt(r).ToString() : "";

        newrow[c] = key + ": " + value;
    }
    dt.Rows.Add(newrow);
}

然后将DataTable绑定到您的DataGridView

dataGridView1.DataSource = dt;

输出: Output