Skip to content

Instantly share code, notes, and snippets.

@lfgrando
Last active July 20, 2018 18:10
Show Gist options
  • Save lfgrando/a4195ab81b9b0a346b4f8441612bc130 to your computer and use it in GitHub Desktop.
Save lfgrando/a4195ab81b9b0a346b4f8441612bc130 to your computer and use it in GitHub Desktop.
Find JTokens by name.
public List<JToken> FindTokens(JToken containerToken, string name)
{
List<JToken> matches = new List<JToken>();
FindTokens(containerToken, name, matches);
return matches;
}
private void FindTokens(JToken containerToken, string name, List<JToken> matches)
{
if (containerToken.Type == JTokenType.Array)
{
foreach (JToken child in containerToken.Children())
{
FindTokens(child, name, matches);
}
}
else if (containerToken.Type == JTokenType.Object)
{
foreach (JProperty child in containerToken.Children<JProperty>())
{
if (child.Name == name)
{
matches.Add(child.Value);
}
FindTokens(child.Value, name, matches);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment