-1

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.

  • Does this answer your question? [How to display list items on console window in C#](https://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp) – Rodrigo Rodrigues Apr 05 '23 at 18:59
  • "Check to make sure the values are correct " Can you pls show the example of validation code? – Serge Apr 05 '23 at 19:06
  • This seems to be more of a Reflection question that anything else. As Serge asked, what type of validation are you attempting to perform? If it is anything complex, where different rules are applied to different properties of the same type, Reflection may not be best choice. – digital_jedi Apr 05 '23 at 19:23
  • What part of this do you need help with? And what is your approach? Console.WriteLine? Debug.WriteLine? string.join? Or do you want to "check if values are correct" interactively using your debugger? – Wyck Apr 05 '23 at 19:50

2 Answers2

0

if you want just to show all data at the screen the easiest way is

   var jsonResults = JsonConvert.DeserializeObject<List<Parameters>>(jsonFile);

    var json = JsonConvert.SerializeObject(jsonResults, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    })

    Console.WriteLine(json);
Serge
  • 40,935
  • 4
  • 18
  • 45
0

First off, here is a solution to your question:

void Main()
{
    string jsonFile = File.ReadAllText(path);
    var jsonResults = JsonConvert.DeserializeObject<List<Parameters>>(jsonFile);

    foreach (var result in jsonResults)
    {
        // Filter the properties of the object by type to get our List<int>s
        var intLists = result.GetType()
            .GetProperties()
            .Where(x => x.PropertyType == typeof(List<int>))
            .ToList();

        foreach (var intList in intLists)
        {   
            // Attempt a safe cast with the "is" operator, which returns true if the 
            // cast succeeds and the result is not null
            if (intList.GetValue(result) is List<int> list)
            {
                Console.WriteLine($"{intList.Name}: {string.Join(',', list)}");
            }
        }
    }
}

While that solution will work to allow you to print out the contents of each List<int> property from each Parameters 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.

breadswonders
  • 41
  • 1
  • 6