I am inputting information from a json file and then need to check that the information is correct. I am not sure how to print out the values of a List from an object. Not all objects have the same properties, I also attached my json file.
Here is my code:
public Parameters()
{
public List<int> ShortPeriodData { get; set; }
public List<int> LongPeriodData { get; set; }
public List<int> UponTriggerData { get; set; }
public string LogRecordType { get; set; }
public int? PowerVariance { get; set; }
public List<double> BatteryData { get; set; }
public List<int> MeanData { get; set; }
public List<int> MeanVariance{ get; set; }
public int? PowerMean{ get; set; }
public string TriggerType{ get; set; }
}
public main()
{
string jsonFile = File.ReadAllText(path);
var jsonResults = JsonConvert.DeserializeObject<List<Parameters>>(jsonFile);
foreach (var result in jsonResults)
{
foreach (PropertyInfo prop in result.GetType().GetProperties())
{
if (prop.GetValue(result, null) != null)
{
//Check to make sure the values are correct
}
}
}
}
json file:
[
{
"ShortPeriodData ":[0,0,0,0]
},
{
"LongPeriodData":[0,0,0,0],
"UponTriggerData":[0,0,0,0],
"LogRecordType":"Power"
},
{
"PowerVariance":5
},
{
"BatteryData ":[0.7,0.3,1.4,5.7]
},
{
"MeanData":[65,71,81,89,98,95],
"MeanVariance":[8,4,4,3,8,5],
"PowerMean":64,
"TriggerType":"PowerUp"
}
]
This is only a sample of the data that I am getting from the json file, in reality it is 100 different parameters that I am reading in. I don’t know how to make a simple way to loop through and check to make sure the data is correct. I have tried to make a switch statement going through all of them but that is a lot of code. Please help.
2
Answers
if you want just to show all data at the screen the easiest way is
First off, here is a solution to your question:
While that solution will work to allow you to print out the contents of each
List<int>
property from eachParameters
object, it is totally unclear how you intend to "check that the information is correct". If you need to implement different validation logic for each list of ints, or each group of parameters, using reflection is an exceptionally poor idea here.If you can provide some more details or clarity on what these parameters mean, what you need to validate, and what you intent to use said parameters for, we may be able to give you a better solution.