I have received a Json file which I have to deal with. I have no idea over the structure. My goal is to deserialize the Json and write a function which allows me to iterate through all the given objects and print certain values. File looks like this:
{
"stream": {
"time": [ 0, 1, 2 ],
"objects": {
"o1": {
"rot_1": [ 3.7, 3.9, 2.1 ],
"rot_2": [ 1.5, 1.7, 0 ],
"rot_3": [ 3, 4, 5 ]
},
"o2": {
"rot_1": [ 5, 6, 7 ],
"rot_2": [ 8, 9, 10 ],
"rot_3": [ 11, 12, 13 ]
}
}
}
Now I am using Newtonsoft.Json to deserializing as a class like this:
public class O1
{
public List<double> rot_1 { get; set; }
public List<double> rot_2 { get; set; }
public List<double> rot_3 { get; set; }
}
public class O2
{
public List<double> rot_1 { get; set; }
public List<double> rot_2 { get; set; }
public List<double> rot_3 { get; set; }
}
public class Objects
{
public O1 o1 { get; set; }
public O2 o2 { get; set; }
}
public class Stream
{
public List<double> time { get; set; }
public Objects objects { get; set; }
}
public class Root
{
public Stream stream { get; set; }
}
class Program
{
static void Main(string[] args)
{
var x = JsonConvert.DeserializeObject<Root>(File.ReadAllText("E:\\TestJson.json"));
foreach (PropertyInfo info in x.stream.objects.GetType().GetProperties())
{
// print:
// o1 -> rot_1 -> 3.7, 3.9, 2.1
// o2 -> rot_1 -> 5, 6, 7
// ....
}
}
}
I have been trying to make PropertyInfo work, but I can't find a way to go "deeper". I have a feeling this might be a pretty trivial question... Sorry I am a noob ...
[JsonProperty("objects")] public Dictionary<string, Dictionary<string, List<double>>> Objects { get; set; }instead ofpublic Objects objects { get; set; }