我试图解析使用json.net一个JSON文件。 该文件看起来像这样
{X:
{
Title:"foo",
xxxx:xxxx
}
}
{Y:
{ZZ:
{Title: "bar",...}
}
}
我想这种结构处理的所有对象递归下降了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)
{
...
}
});
}
下面的代码应该是相当接近你在找什么。 我提出的假设,即有一个外部阵列,并且阵列可以在层次结构中的任何地方出现。 (如果这是不正确的,可以简化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);
}
}
}
}
}
此外,还需要做各种各样的事情。 想提出我的解决方案。 它的优点是:
- 不是递归
- 没有回调
- 不假定任何内部结构(阵列)
解耦树遍历从需要执行的操作
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); } } }
然后你可以使用LINQ过滤和执行操作:
var tokens = AllTokens(jsonObj); var titles = tokens.Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "Title");
我想我会包括我小的调整,@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);
}
}
}
然后,OP可以这样做:
WalkNode(json, null, prop =>
{
if (prop.Name == "Title" && prop.Value.Type == JTokenType.String)
{
string title = prop.Value<string>();
Console.WriteLine(title);
}
});
试试这个方法我有些不成功的尝试后写它:
private void Traverse(JToken token, TreeNode tn)
{
if (token is JProperty)
if (token.First is JValue)
tn.Nodes.Add(((JProperty)token).Name + ": " + ((JProperty)token).Value);
else
tn = tn.Nodes.Add(((JProperty)token).Name);
foreach (JToken token2 in token.Children())
Traverse(token2, tn);
}
你首先必须通过像这样完整的JSON文件:
TreeNode rooty= tvu.Nodes.Add("Rooty") // not the Indian bread,just Rooty, Ok?
JToken token = JToken.Parse(File.ReadAllText(<"Path to json file">));
Traverse(token, rooty);
做完后,你汜得到这个一个:哦,不,我不能嵌入图片。 伤心。