0

I'm getting json from file. It could have a different stucture, for examle it could look like this:

    {
    "root": {
        "name": "LWindow",
        "children": [{
                "name": "Label",
                "children": []
            },
            {
                "name": "Edit",
                "children": []
            },
            {
                "name": "Label",
                "children": []
            },
            {
                "name": "Radio",
                "children": []
            },
            {
                "name": "Checkbox",
                "children": []
            },
            {
                "name": "Label",
                "children": []
            },
            {
                "name": "Label",
                "children": []
            }
        ]
    }
}

or like this:

    {
    "root": {
        "name": "LWindow",
        "children": [{
                "name": "FormItem",
                "children": [{
                        "name": "Label",
                        "children": []
                    },
                    {
                        "name": "Edit",
                        "children": []
                    }
                ]
            },
            {
                "name": "FormItem",
                "children": [{
                        "name": "Label",
                        "children": []
                    },
                    {
                        "name": "Edit",
                        "children": []
                    }
                ]
            },
            {
                "name": "FormItem",
                "children": [{
                        "name": "Label",
                        "children": []
                    },
                    {
                        "name": "Radio",
                        "children": []
                    }
                ]
            },
            {
                "name": "Label",
                "children": []
            },
            {
                "name": "Button",
                "children": []
            }
        ]
    }
}

What I need is to deserialize it to c# objects. I've already got a class, that describes json:

public partial class Root
{
    public RootElement RootRoot { get; set; }
}

public partial class RootElement
{
    public string Name { get; set; }
    public List<RootElement> Children { get; set; }
}

But I don't really understand how to extract nested objects from my RootElement because nested objects could have different structure and could have their own nested objects.

I forgot to metion. I've alredy deserialized json to my Root with:

public static T DeserializeJson<T>(String pathToJSON)
    {
        using (StreamReader file = File.OpenText(pathToJSON))
        {
            JsonSerializer serializer = new JsonSerializer();
            return (T)serializer.Deserialize(file, typeof(T));
        }
    }
kuvandyk
  • 13
  • 3
  • 2
    What did you try? Did you search for "deserialize json to C#"? I bet there are hundreds, if not thousands, of questions. At least one of them pretty sure gives you what you´re looking for. – MakePeaceGreatAgain May 04 '21 at 07:31
  • To deseralize into a dynamic object see https://stackoverflow.com/Questions/4535840 to conver to a specific class see https://stackoverflow.com/questions/25052293 – Daveo May 04 '21 at 07:38
  • Does this answer your question? [Deserialize JSON to C# Classes](https://stackoverflow.com/questions/25052293/deserialize-json-to-c-sharp-classes) – Daveo May 04 '21 at 07:38
  • Thanks. I've already deserialized my json file to Root class. The problem is to exctract nested objects from this class. – kuvandyk May 04 '21 at 07:52

1 Answers1

0

I seems that your model is corresponding to the JSON structure you might get. In such case, you only need to read the file and perform the deserialization. It should be something like this:

string json = File.ReadAllText("File path");
Root root = JsonSerializer.Deserialize<Root>(json);

Once you have the Root object, you can check if it has children at any level.

Update: To traverse the children, you should add a recursive method like this:

private void Traverse(RootElement element)
{
    foreach (var child in element.Children)
    {
        // Do something with the child

        this.Traverse(child); // Traverse the child recursively
    }
}
  • Yeah, I forgot to mention. I've already got my rootobject class. Did it with this method public static T DeserializeJson(String pathToJSON) { using (StreamReader file = File.OpenText(pathToJSON)) { JsonSerializer serializer = new JsonSerializer(); return (T)serializer.Deserialize(file, typeof(T)); } } – kuvandyk May 04 '21 at 07:46
  • In such case, it seems you need a recursive method to walk over the children, and for every child, call the same recursive method. – Yevgeny Efter May 04 '21 at 08:44
  • Added an example of how to traverse the elements tree recursively. – Yevgeny Efter May 04 '21 at 12:13