pm1*_*100 11 c# recursion recursive-descent json.net
我试图使用json.net解析json文件.该文件看起来像这样
{X:
{
Title:"foo",
xxxx:xxxx
}
}
{Y:
{ZZ:
{Title: "bar",...}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过Title属性递归处理所有对象的结构.但我感到困惑JToken,JProperty,JContainer,JValue,JObject.阅读源代码并没有让我更加明智,也没有任何样本有帮助.我想要的东西是这样的
WalkNode(node, Action<Node> action)
{
foreach(var child in node.Children)
{
Action(child);
WalkNode(child);
}
}
Parse()
{
WalkNode(root, n=>
{
if(n["Title"] != null)
{
...
}
});
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*ers 17
下面的代码应该与您正在寻找的代码非常接近.我假设有一个外部数组,并且数组可以出现在层次结构中的任何位置.(如果不是这样,您可以稍微简化WalkNode方法代码,但它应该以任何一种方式工作.)
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonRecursiveDescent
{
class Program
{
static void Main(string[] args)
{
string json =
@"[
{
""X"":
{
""Title"":""foo"",
""xxxx"":""xxxx""
}
},
{
""Y"":
{
""ZZ"":
{
""Title"":""bar"",
""xxxx"":""xxxx""
}
}
}
]";
JToken node = JToken.Parse(json);
WalkNode(node, n =>
{
JToken token = n["Title"];
if (token != null && token.Type == JTokenType.String)
{
string title = token.Value<string>();
Console.WriteLine(title);
}
});
}
static void WalkNode(JToken node, Action<JObject> action)
{
if (node.Type == JTokenType.Object)
{
action((JObject)node);
foreach (JProperty child in node.Children<JProperty>())
{
WalkNode(child.Value, action);
}
}
else if (node.Type == JTokenType.Array)
{
foreach (JToken child in node.Children())
{
WalkNode(child, action);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
The*_*dge 14
还需要做一些事情.想提出我的解决方案.它的优点是:
将树遍历与需要执行的操作分离
IEnumerable<JToken> AllTokens(JObject obj) {
var toSearch = new Stack<JToken>(obj.Children());
while (toSearch.Count > 0) {
var inspected = toSearch.Pop();
yield return inspected;
foreach (var child in inspected) {
toSearch.Push(child);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用linq过滤和执行操作:
var tokens = AllTokens(jsonObj);
var titles = tokens.Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "Title");
Run Code Online (Sandbox Code Playgroud)我以为我会将我的细微调整包括在@BrianRogers WalkNode方法中,这使其用途更加广泛:
private static void WalkNode(JToken node,
Action<JObject> objectAction = null,
Action<JProperty> propertyAction = null)
{
if (node.Type == JTokenType.Object)
{
if (objectAction != null) objectAction((JObject) node);
foreach (JProperty child in node.Children<JProperty>())
{
if (propertyAction != null) propertyAction(child);
WalkNode(child.Value, objectAction, propertyAction);
}
}
else if (node.Type == JTokenType.Array)
{
foreach (JToken child in node.Children())
{
WalkNode(child, objectAction, propertyAction);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,OP可以执行以下操作:
WalkNode(json, null, prop =>
{
if (prop.Name == "Title" && prop.Value.Type == JTokenType.String)
{
string title = prop.Value<string>();
Console.WriteLine(title);
}
});
Run Code Online (Sandbox Code Playgroud)
你也可以用 JSONPath 做到这一点: node.SelectTokens("$..*");
像这样使用:
var jObjectsWithTitle = node
.SelectTokens("$..*")
.OfType<JObject>()
.Where(x => x.Property("Title") != null);
Run Code Online (Sandbox Code Playgroud)
要不就:
var jObjectsWithTitle = node.SelectTokens("$..[?(@.Title)]");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14992 次 |
| 最近记录: |