0

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;
}
13
  • Are you using Newtonsoft JSON library? Commented May 16, 2022 at 15:29
  • Post your JSON. Commented May 16, 2022 at 15:29
  • You should specify what fill the application means and what kind of application you're building. Commented May 16, 2022 at 15:29
  • if using newtonsoft, deserialize just to a JObject and use the JObject methods to go through properties Commented May 16, 2022 at 15:29
  • 2
    Just give us a minimal reproducible example. Doesn't need to have any resemblance to your actual data as long as it shows the issue. The code shown won't do it, I am afraid. Commented May 16, 2022 at 15:31

2 Answers 2

5

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;}
    
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this but the rootProperties are empty
Have you stepped through it with the debugger and verified that it does have data after its deserialized and before you start looping through its properties? Set a breakpoint on the line you declare JsonDecoded and expand its properties. If you want to share an example json file, I can help you with that.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.