Reputation: 117
I have a json file (text) that has 908 rows worth of data, with multiple hardware Class types.
Each grouping has these, where Properties changes based on the component type.
"HardwareID": "ACPI\\AuthenticAMD_-_AMD64_Family_16_Model_4",
"Count": "8",
"Class": "Processor",
"Properties": { }
"DeviceType": "Cpu",
"Description": "Quad-Core AMD Opteron(tm) Processor 2373 EE"
I believe the first part is to a Public Class (i.e. Devices), that breakdowns how the json file structured. Is this correct? If so, how do I add the Properties?
Then once loaded, I can deserialize the object in the following manner.
Devices device = JsonConvert.DeserializeObject<Devices>(json);
Am I on the correct path? It was my plan, as I load the different Class(es) [i.e. Processor, HDD, etc] to load them into a specific DataTable. Once the file has been completely evaluated, to the output the different DataTables to a csv file. This will give me the ability to evaluate the different components.
namespace jsonReader
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;
[DataContract]
public class RootObject
{
public class Devices
{
public string HardwareId { get; set; }
public int Count { get; set; }
public string Class { get; set; }
public IList<string> Properties { get; set; }
public string DeviceType { get; set; }
public string Description { get; set; }
}
}
Here is what my json file looks like.
{
"Devices": [
{
"HardwareID": "ACPI\\AuthenticAMD_-_AMD64_Family_16_Model_4",
"Count": "8",
"Class": "Processor",
"Properties": {
"CPUIDBrandString": "Quad-Core AMD Opteron(tm) Processor 2373 EE",
"CPUIDString": "AuthenticAMD",
"CPUIDMultithreading": "1",
"CPUIDBrandID": "0",
"CPUIDExtFamily": "1",
"CPUIDExtModel": "0",
"CPUIDSteppingID": "2",
"CPUIDNumberOfCores": "0",
"CPUIDLogicalCPUCount": "4",
"CPUIDFamily": "15",
"CPUIDVMExt": "0",
"CPUCount": "8",
"CPUIDProcessorID": "1696726757271408450",
"IsNuma": "true",
"CPUIDType": "0",
"Cpu_Temperature": "0.000000",
"CPUIDCapable64bit": "1",
"CPUIDModel": "4"
},
"DeviceType": "Cpu",
"Description": "Quad-Core AMD Opteron(tm) Processor 2373 EE"
},
],
}
Upvotes: 1
Views: 142
Reputation: 5305
I believe the first part is to a Public Class (i.e. Devices), that breakdowns how the json file structured. Is this correct?
Yes it is. Basically you crate a class
which represents your Josn.
If so, how do I add the Properties?
The Properties
in your case is itself a class
, so you should create a class
representing it.
So your classes will look like this:
public class RootObject
{
public Device[] Devices { get; set; }
}
public class Device
{
public string HardwareID { get; set; }
public string Count { get; set; }
public string Class { get; set; }
public Properties Properties { get; set; }
public string DeviceType { get; set; }
public string Description { get; set; }
}
public class Properties
{
public string CPUIDBrandString { get; set; }
public string CPUIDString { get; set; }
public string CPUIDMultithreading { get; set; }
public string CPUIDBrandID { get; set; }
public string CPUIDExtFamily { get; set; }
public string CPUIDExtModel { get; set; }
public string CPUIDSteppingID { get; set; }
public string CPUIDNumberOfCores { get; set; }
public string CPUIDLogicalCPUCount { get; set; }
public string CPUIDFamily { get; set; }
public string CPUIDVMExt { get; set; }
public string CPUCount { get; set; }
public string CPUIDProcessorID { get; set; }
public string IsNuma { get; set; }
public string CPUIDType { get; set; }
public string Cpu_Temperature { get; set; }
public string CPUIDCapable64bit { get; set; }
public string CPUIDModel { get; set; }
}
Note that you don't need too use DataContract
attribute and for deserialize your Json (using Json.NET) you need to write:
Devices device = JsonConvert.DeserializeObject<RootObject>(json);
As a tip, you can easily create these data classes using Visual Studio
's (2012 up) Edit > Paste Special > Paste JSON As Classes menu item. Just copy your Json string data into clip board (Ctrl + C) then use Paste Special.
Upvotes: 2