Home >Backend Development >C++ >How to Efficiently Find Specific JTokens by Name within a Nested JObject Hierarchy?

How to Efficiently Find Specific JTokens by Name within a Nested JObject Hierarchy?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-31 12:16:10629browse

How to Efficiently Find Specific JTokens by Name within a Nested JObject Hierarchy?

Searching for a Specific JToken by Name in a JObject Hierarchy

Background:

When dealing with complex JSON structures, it's often necessary to retrieve specific values based on their token names. This can be challenging without a built-in method.

Built-in Method:

Unfortunately, the NewtonsoftJson library does not provide a direct method to retrieve a JToken by name.

Recursive Method:

To overcome this limitation, a recursive method can be implemented to search for JTokens by name within a JObject hierarchy. Here's an example:

public static class JsonExtensions
{
    public static List<JToken> FindTokens(this JToken containerToken, string name)
    {
        List<JToken> matches = new List<JToken>();
        FindTokens(containerToken, name, matches);
        return matches;
    }

    private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
    {
        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);
            }
        }
        else if (containerToken.Type == JTokenType.Array)
        {
            foreach (JToken child in containerToken.Children())
            {
                FindTokens(child, name, matches);
            }
        }
    }
}

Demo:

Here's an example demonstrating the usage of this extension method:

string json = @"
{
    ""routes"": [
        {
            ""bounds"": {
                ""northeast"": {
                    ""lat"": 50.4639653,
                    ""lng"": 30.6325177
                },
                ""southwest"": {
                    ""lat"": 50.4599625,
                    ""lng"": 30.6272425
                }
            },
            ""legs"": [
                {
                    ""distance"": {
                        ""text"": ""1.7 km"",
                        ""value"": 1729
                    },
                    ""duration"": {
                        ""text"": ""4 mins"",
                        ""value"": 223
                    }
                },
                {
                    ""distance"": {
                        ""text"": ""2.3 km"",
                        ""value"": 2301
                    },
                    ""duration"": {
                        ""text"": ""5 mins"",
                        ""value"": 305
                    }
                }
            ]
        }
    ]
}";

JObject jo = JObject.Parse(json);

foreach (JToken token in jo.FindTokens("text"))
{
    Console.WriteLine(token.Path + ": " + token.ToString());
}

Output:

routes[0].legs[0].distance.text: 1.7 km
routes[0].legs[0].duration.text: 4 mins
routes[0].legs[1].distance.text: 2.3 km
routes[0].legs[1].duration.text: 5 mins

The above is the detailed content of How to Efficiently Find Specific JTokens by Name within a Nested JObject Hierarchy?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn