简体   繁体   English

遍历来自 json c# 的嵌套对象

[英]Iterate through nested objects from json c#

I have received a Json file which I have to deal with.我收到了一个必须处理的 Json 文件。 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.我的目标是反序列化 Json 并编写 function ,它允许我遍历所有给定的对象并打印某些值。 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:现在我正在使用 Newtonsoft.Json 反序列化为 class ,如下所示:

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".我一直在尝试使 PropertyInfo 工作,但我找不到 go “更深”的方法。 I have a feeling this might be a pretty trivial question... Sorry I am a noob...我有一种感觉,这可能是一个非常微不足道的问题......对不起,我是一个菜鸟......

Change your object to:将您的 object 更改为:

public partial class Root
{
    [JsonProperty("stream")]
    public Stream Stream { get; set; }
}

public partial class Stream
{
    [JsonProperty("time")]
    public List<long> Time { get; set; }

    [JsonProperty("objects")]
    public Dictionary<string, Dictionary<string, List<double>>> Objects { get; set; }
}

Iterating througth it like:像这样迭代它:

foreach(var obj in  result.Stream.Objects){
    Console.WriteLine($"object name : [{obj.Key}]");
    foreach(var element in obj.Value){
        Console.WriteLine($"  -> element name : [{element.Key}]");
        foreach(var val in element.Value){              
            Console.WriteLine($"    -> value: [{val}]");
        }
    }
    Console.WriteLine();
}

Result:结果:

object name : [o1]
  -> element name : [rot_1]
    -> value: [3.7]
    -> value: [3.9]
    -> value: [2.1]
  -> element name : [rot_2]
    -> value: [1.5]
    -> value: [1.7]
    -> value: [0]
  -> element name : [rot_3]
    -> value: [3]
    -> value: [4]
    -> value: [5]

object name : [o2]
  -> element name : [rot_1]
    -> value: [5]
    -> value: [6]
    -> value: [7]
  -> element name : [rot_2]
    -> value: [8]
    -> value: [9]
    -> value: [10]
  -> element name : [rot_3]
    -> value: [11]
    -> value: [12]
    -> value: [13]

Live demo: https://dotnetfiddle.net/5QIeHJ现场演示: https://dotnetfiddle.net/5QIeHJ

There are many ways to skin this cat.有很多方法可以给这只猫剥皮。

There is the dictionary or dictionaries way that others suggested and there is the crude way that will work OK if it's always two o s and 3 rot_ s:有其他人建议的字典或字典方式,如果总是两个o s 和 3 rot_ s,那么粗略的方式可以正常工作:

static void WriteLine(string name, List<double> l) => Console.WriteLine($"{name} -> {string.Join(",", l)}");

// Option 1:
WriteLine("o1 -> rot_1", x.stream.objects.o1.rot_1);
WriteLine("o1 -> rot_2", x.stream.objects.o1.rot_2);
...

It prints what it needed:它打印出它需要的东西:

o1 -> rot_1 -> 3.7,3.9,2.1
o1 -> rot_2 -> 1.5,1.7,0
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM