循环动态JSON以获取所有节点C#

时间:2018-10-09 13:13:21

标签: c# json

我需要循环播放多个JSON文件并从中获取某些详细信息。但是,我希望有一个大小适合所有循环,因为子节点在属性方面相互匹配。谁能建议我如何循环JSON节点?

示例:

{
    "name": "Example",
    "description": "Example JSON",
    "properties":  {
         "foo": "bar",
         "foo1": "bar2",
         "foo3": "bar4",
    },
    "stages":  {
         "This is a stage": {
              "stageInfo1": "blah",
              "stageInfo2": "blah",
              "integration": {
                  "x": "x",
                  "y": "y",
                  "z": "z"
              }
         },
         "Another Stage": {
              "stageInfo1": "blah",
              "stageInfo2": "blah",
              "integration": {
                  "x": "x",
                  "y": "y",
                  "z": "z"
              }
         }
    }
 }

可以有数百个阶段。但是JSON的模式遵循这种通用模式,阶段可以具有随机名称,但是它们包含相同的定义。

有什么简单的建议吗?

2 个答案:

答案 0 :(得分:3)

只需将数据建模到类中,然后使用诸如Newtonsoft.JSON之类的反序列化库

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

public class Program
{
    public class Stage
    {
        [JsonProperty("stageInfo1")]
        public string StageInfo1;
    }

    public class JsonData
    {
        [JsonProperty("stages")]
        public Dictionary<string, Stage> Stages;
    }

    public static void Main()
    {
        var json = @"{
    ""name"": ""Example"",
    ""description"": ""Example JSON"",
    ""properties"":  {
         ""foo"": ""bar"",
         ""foo1"": ""bar2"",
         ""foo3"": ""bar4"",
    },
    ""stages"":  {
         ""This is a stage"": {
              ""stageInfo1"": ""blah"",
              ""stageInfo2"": ""blah"",
              ""integration"": {
                  ""x"": ""x"",
                  ""y"": ""y"",
                  ""z"": ""z""
              }
         },
         ""Another Stage"": {
              ""stageInfo1"": ""blah"",
              ""stageInfo2"": ""blah"",
              ""integration"": {
                  ""x"": ""x"",
                  ""y"": ""y"",
                  ""z"": ""z""
              }
         }
    }
 }";
        var data = JsonConvert.DeserializeObject<JsonData>(json);
        Console.WriteLine(data.Stages.Keys.ToArray());
    }
}

答案 1 :(得分:1)

NewtonSoft JObject将为您完成繁重的工作。它使用LINQable接口将json文档包装为一种动态对象。 .Net项目的大多数模板都将依赖提供它的https://www.nuget.org/packages/Newtonsoft.Json软件包的NuGet依赖项。

void Main()
{
    var json = @"{
        ""name"": ""Example"",

        ""description"": ""Example JSON"",
        ""properties"":  {
        ""foo"": ""bar"",
             ""foo1"": ""bar2"",
             ""foo3"": ""bar4"",
        },
        ""stages"":  {
        ""This is a stage"": {
            ""stageInfo1"": ""blah"",
                  ""stageInfo2"": ""blah"",
                  ""integration"": {
                ""x"": ""x"",
                      ""y"": ""y"",
                      ""z"": ""z""

                  }
        },
             ""Another Stage"": {
            ""stageInfo1"": ""blah"",
                  ""stageInfo2"": ""blah"",
                  ""integration"": {
                ""x"": ""x"",
                      ""y"": ""y"",
                      ""z"": ""z""

                  }
        }
    }
     }";

    var jo = JObject.Parse(json);

    Console.WriteLine("A couple of ways to access just one level of ( Path,Value(s) ) pairs --------------------------------------------");
    foreach (var node in jo) { Console.WriteLine("{0} {1}", node.Key, node.Value); }
    Console.WriteLine("--- Or this --------------------------------------------");
    foreach (var jtoken in jo.Children()) { Console.WriteLine("{0}={1} | has {2} children", jtoken.Path, string.Join(",\n", jtoken.Values()),  jtoken.Children().Count()); };

    Console.WriteLine("\n\n------------------- But to walk the full tree, use recursion----------------------------------------\n");
    WriteRecursively(jo);
}

void WriteRecursively(JToken topJToken)
{
    foreach (var jtoken in topJToken.Children())
    {
        Console.WriteLine("{0}={1} | has {2} children", jtoken.Path, string.Join(",\n", jtoken.Values()), jtoken.Children().Count());
        WriteRecursively(jtoken);
    };
}

输出:

A couple of ways to access just one level of ( Path,Value(s) ) pairs --------------------------------------------
    name Example
    description Example JSON
    properties {
      "foo": "bar",
      "foo1": "bar2",
      "foo3": "bar4"
    }
    stages {
      "This is a stage": {
        "stageInfo1": "blah",
        "stageInfo2": "blah",
        "integration": {
          "x": "x",
          "y": "y",
          "z": "z"
        }
      },
      "Another Stage": {
        "stageInfo1": "blah",
        "stageInfo2": "blah",
        "integration": {
          "x": "x",
          "y": "y",
          "z": "z"
        }
      }
    }
    --- Or this --------------------------------------------
    name=Example | has 1 children
    description=Example JSON | has 1 children
    properties="foo": "bar",
    "foo1": "bar2",
    "foo3": "bar4" | has 1 children
    stages="This is a stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    },
    "Another Stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    } | has 1 children


    ------------------- But to walk the full tree, use recursion----------------------------------------

    name=Example | has 1 children
    name= | has 0 children
    description=Example JSON | has 1 children
    description= | has 0 children
    properties="foo": "bar",
    "foo1": "bar2",
    "foo3": "bar4" | has 1 children
    properties=bar,
    bar2,
    bar4 | has 3 children
    properties.foo=bar | has 1 children
    properties.foo= | has 0 children
    properties.foo1=bar2 | has 1 children
    properties.foo1= | has 0 children
    properties.foo3=bar4 | has 1 children
    properties.foo3= | has 0 children
    stages="This is a stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    },
    "Another Stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    } | has 1 children
    stages={
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    },
    {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z"
      }
    } | has 2 children
    stages['This is a stage']="stageInfo1": "blah",
    "stageInfo2": "blah",
    "integration": {
      "x": "x",
      "y": "y",
      "z": "z"
    } | has 1 children
    stages['This is a stage']=blah,
    blah,
    {
      "x": "x",
      "y": "y",
      "z": "z"
    } | has 3 children
    stages['This is a stage'].stageInfo1=blah | has 1 children
    stages['This is a stage'].stageInfo1= | has 0 children
    stages['This is a stage'].stageInfo2=blah | has 1 children
    stages['This is a stage'].stageInfo2= | has 0 children
    stages['This is a stage'].integration="x": "x",
    "y": "y",
    "z": "z" | has 1 children
    stages['This is a stage'].integration=x,
    y,
    z | has 3 children
    stages['This is a stage'].integration.x=x | has 1 children
    stages['This is a stage'].integration.x= | has 0 children
    stages['This is a stage'].integration.y=y | has 1 children
    stages['This is a stage'].integration.y= | has 0 children
    stages['This is a stage'].integration.z=z | has 1 children
    stages['This is a stage'].integration.z= | has 0 children
    stages['Another Stage']="stageInfo1": "blah",
    "stageInfo2": "blah",
    "integration": {
      "x": "x",
      "y": "y",
      "z": "z"
    } | has 1 children
    stages['Another Stage']=blah,
    blah,
    {
      "x": "x",
      "y": "y",
      "z": "z"
    } | has 3 children
    stages['Another Stage'].stageInfo1=blah | has 1 children
    stages['Another Stage'].stageInfo1= | has 0 children
    stages['Another Stage'].stageInfo2=blah | has 1 children
    stages['Another Stage'].stageInfo2= | has 0 children
    stages['Another Stage'].integration="x": "x",
    "y": "y",
    "z": "z" | has 1 children
    stages['Another Stage'].integration=x,
    y,
    z | has 3 children
    stages['Another Stage'].integration.x=x | has 1 children
    stages['Another Stage'].integration.x= | has 0 children
    stages['Another Stage'].integration.y=y | has 1 children
    stages['Another Stage'].integration.y= | has 0 children
    stages['Another Stage'].integration.z=z | has 1 children
    stages['Another Stage'].integration.z= | has 0 children