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

标签 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 的模式遵循这个一般模式,阶段可以有随机名称,但它们包含相同的定义。

有什么简单的建议吗?

最佳答案

NewtonSoft JObject将为您完成繁重的工作。它将 json 文档包装为一种具有 LINQable 接口(interface)的动态对象。 .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

关于c# - 循环动态 JSON 以获取所有节点 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52721960/

相关文章:

c# - Entity Framework 负载排序

c# - C# 中的 bool 赋值运算符

c# - 两个 DataGridView;排列列

javascript - Ember 应用程序无法从本地文件加载 json 数据

javascript - 使用 Angular JS for WordPress JSON API 通过 JSON 操作数据

c# - c#中字符串的反转部分

arrays - 将JSON中的多个数组对象解析为CSV jq的JQ错误:错误(在<stdin>:734665):无法使用字符串索引数组

python - 如何在 Python 中正确转义 json 字符串中的双引号 (")

java - 如何让 XStream 映射回类型化列表?

c# - 在 C# 应用程序中安全存储 Azure Blob 存储访问 key 的正确方法