Steevan
Steevan

Reputation: 11

C# Loop trough every property in class

I've made a class containing everything from the json file. Now I want to loop trough every object within this object so I can for fill the application with those values.

I want to scroll trough every CM0xx and use that description: I want to scroll trough every CM0xx and use that description

I hope my goal is clear with that screenshot.

I know that I have to do something like

foreach(CM0XX step in Stepsss)

but that simply wont work. If making a list<string[]> for it is easier from a json but im clueless for the solution. this is the code I have now. And the json file ive converted is generated so that should be fine.

string testdata = File.ReadAllText(Application.StartupPath + "\\TestData\\SSH.json");
Root JsonDecoded = JsonConvert.DeserializeObject<Root>(testdata);
testSteps Stepsss = JsonDecoded.SmartShoulder.TestSteps;
                     
foreach (testSteps step in Stepsss)
{
}

this is part of the json. there are alot more CM012


    public class CM181
    {
        public string Name;
        public string description;
        public string minValue;
        public string maxValue;
        public string unit;
        public string instructions;
        public string prerequisites;
    }

    public class Root
    {
        public kkkk kk;
    }

    public class kkkk
    {
        public string preset;
        public testSteps TestSteps;
    }

    public class testSteps
    {
        public CM011 CM011;
}

Upvotes: 0

Views: 4731

Answers (2)

Geoduck
Geoduck

Reputation: 8995

Making a class to deserialize to is generally done to avoid the need to loop through all the properties. You've defined all the properties in the class, so you can just use them. It's a lot of overhead to use reflection to put all the values into a class, then again using reflection to pull them back out. That's why Newtonsoft has JToken/JObject/JArray classes.

using Newtonsoft.Json.Linq;

...
// parse your JSON
var jo = JObject.Parse(jsonString);

// go through all properties
// just for an example, I put them all in a dictionary

var dict = new Dictionary<String, JToken>()
foreach( JProperty p in jo.Properties() ) {
    dict.Add(p.Name, p.Value)
}

Now, each Value is a JToken, which could actually be another JObject, or a JArray or just a simple token. You can check JToken.Type to see what type the property really has, and do something logical accordingly.

Upvotes: 0

Dan Csharpster
Dan Csharpster

Reputation: 2732

You could use reflection to loop through the names and the values. If you just need the items under TestSteps, this should work. If you need a full hierarchy, that is a bit more involved. Here is an example below and the fiddle to it:

using System;
using System.Reflection;
                
public class Program
{
    public static void Main()
    {
        var record = new Root()
        {
            Foo = "foo",
            Bar = "bar"
        };

        PropertyInfo[] rootProperties = typeof(Record).GetProperties();
        foreach (PropertyInfo property in rootProperties)
        {
            var value = property.GetValue(record);
            Console.WriteLine(property.Name + " - " + value);
            
        }
    }
}

public class Root
{
    public string Foo {get;set;}
    public string Bar {get;set;}
    
}

Upvotes: 5

Related Questions