Last active
July 20, 2018 18:10
-
-
Save lfgrando/a4195ab81b9b0a346b4f8441612bc130 to your computer and use it in GitHub Desktop.
Find JTokens by name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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