I'm trying to read a .json file and in my C# project and count the amount of objects inside an other object.
The .json file is fix and I'm not able to change anything from it.
e.g.
The .json file looks like this:
{
"X": {
"x": "...",
"x1": "..."
},
"Y": {
"y": { }
"y1": { },
"y2": { },
"y3": { },
"y4": { },
"functiongroup": {
"function1": {
"A": "...",
"B": "...",
"C": "...",
"D": "...",
"subfunction": {
"sub1": {
"a": "...",
"b": "...",
"c": "..."
}
}
},
"function2": {
"A": "...",
"B": "...",
"C": "...",
"D": "...",
"subfunction": {
"sub1": {
"a": "...",
"b": "...",
"c": "...",
"d": "..."
},
"sub2": {
"a": "...",
"b": "...",
"d": "..."
},
"sub3": {
"a": "...",
"b": "...",
"c": "..."
}
}
},
"function3": {
"A": "...",
"B": "...",
"C": "...",
"D": "...",
"subfunction": {
"sub1": {
"a": "...",
"b": "...",
"c": "...",
"d": "..."
},
"sub2": {
"a": "...",
"b": "...",
"d": "..."
},
"sub3": {
"a": "...",
"b": "...",
"c": "..."
}
}
},
"function2": {
"A": "...",
"B": "...",
"C": "...",
"D": "...",
"subfunction": {
"sub1": {
"a": "...",
"b": "...",
"c": "...",
"d": "..."
},
"sub2": {
"a": "...",
"b": "...",
"d": "..."
},
"sub3": {
"a": "...",
"b": "...",
"c": "...""
},
sub4": {
"a": "...",
"b": "...",
"c": "..."
}
}
}
}
}
Now i want to know how many subfunctions there are in every function and print the amount in the Console. So in the shown example it would look like this:
OUTPUT
1, 3, 3, 4
I´ve tried multiple variants like
var JsonFile = System.IO.File.ReadAllText(@"PathtoFile");
var token = JToken.Parse(JsonFile);
var FG = token.Value<JArray>("subfunction");
int count = FG.Count();
Console.WriteLine(count);
usingdirectives aren't in your question, I can't be sure, so I won't add the tag myself.[]){or[and it is missing some closing tags. Can you please provide a valid file?