Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Program
{
static void Main(string[] args)
{
string json =
@"[
{
""X"":
{
""Title"":""foo"",
""xxxx"":""xxxx""
},
""X"":
{
""twat"":""step"",
""ass"":""b""
}
},
{
""Y"":
{
""ZZ"":
{
""Title"":""bar"",
""xxxx"":""xxxx""
}
}
}
]";
JToken node = JToken.Parse(json);
WalkNode(node, n =>
{
foreach (var property in n.Properties().Where(x => x.Value.Type == JTokenType.String))
{
Console.Write(property.Name + " - > ");
Console.WriteLine(property.Value);
}
//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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment