json 将对象/动态作为DataGridView放置

1yjd4xko  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(120)

解密后我得到了一个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

好了,现在我想设置头部信息,如“硬件信息”,“系统信息”和“环境变量”作为一列,并在他们下面的一行中的其他信息。
因为总是会有一些新的列或行,所以代码独立于我工作是很重要的(比如不为数据添加一个新类)
我试过使用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已格式化。

该表应如下所示:

谢谢你的帮助。

nnt7mjpx

nnt7mjpx1#

首先将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;

输出:

irlmq6kh

irlmq6kh2#

我想得到这个json(在C#中)enter image description here的RESULTS部分,我只展示了其中的一行,但它是一个数组:
{“count”:12,“next”:null,“previous”:null,“results”:[ {“_id”:374666939,“formhub/uuid”:“6c 48 d4 fc26434 ab 69 c188520930 b1407”,“开始”:“2022-12-15T15:41:11.541+01:00”,“end”:“2022-12-17T10:15:28.512+01:00”,“deviceid”:“collect:RmC8NKklTOIvN3pw”,“grp_1/presence_formation_lutte_violence”:“Oui”,“grp_1/commentaire_presence_formation_lutte_violence”:“En novembre 2022”,“grp_1/presence_prestataire_formation_clinique”:“Oui”,“grp_1/pep_disponible”:“Oui”,“grp_1/commentaire_pep_disponible”:“无破裂”,“grp_1/dose_pep_disponible”:“Oui”,“_validation_status”:{},“_submitted_by”:“admin_apef”} ] }

相关问题