Reputation: 477
Well I got a JSON String after decrypting which looks like
"{\"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"
The formatted JSON String is
{
"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
Well, now I want to set the head information like "HardwareInformation", "SystemInformation" and "EnvironmentVariables" as a column and under them in a row the other information.
Because there could always come some new columns or rows, it's important that the code is working independently from me (Like not adding a new class just for the data)
I've tried using Newtonsoft.JSON
and got the JSON in dynamic
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==");
(Please ignore the decrypt) and as a JObject
JObject json1 = JObject.Parse(json);
The dynamic is not formatted and the JObject is.
The table should look like this:
Thanks for your help.
Upvotes: 2
Views: 782
Reputation: 2437
First, deserialize the json into Dictionary<string, Dictionary<string, object>>
. Count number of rows and columns. Add columns and Rows.
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);
}
Then bind the DataTable
to your DataGridView
dataGridView1.DataSource = dt;
Upvotes: 3