gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:27:46 24 4
gpt4 key购买 nike

我有多个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com