在 JObject 层次结构中按名称搜索特定的 JToken

41

我从服务器上收到了一些JSON响应,例如:

{"routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 50.4639653,
           "lng" : 30.6325177
        },
        "southwest" : {
           "lat" : 50.4599625,
           "lng" : 30.6272425
        }
     },
     "copyrights" : "Map data ©2013 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "1.7 km",
              "value" : 1729
           },
           "duration" : {
              "text" : "4 mins",
              "value" : 223
           },

我想从中获取令牌“text”的值

      "legs" : [
        {
           "distance" : {
              "text" : "1.7 km",
              "value" : 1729
           },

这是一个字符串,其值为“1.7 km”。

问题:在NewtonsoftJson库中是否有任何内置函数类似于:

public string(or JToken) GetJtokenByName(JObject document, string jtokenName)

或者我需要实现一些递归方法,在JObject中的所有JToken和JArray中按名称搜索JToken吗?

3个回答

98

如果你正在寻找一个非常具体的令牌并知道它的路径,你可以使用内置的SelectToken()方法轻松导航到它。例如:

如果您正在寻找非常特定的令牌并且知道路径,可以使用内置的SelectToken()方法轻松导航到该令牌。例如:

string distance = jObject.SelectToken("routes[0].legs[0].distance.text").ToString();

如果您需要查找JSON中给定名称的所有令牌出现次数,无论它们在哪里发生,那么是的,您需要一个递归方法。这是一个可能起作用的方法:

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);
            }
        }
    }
}

这里是一个演示:

class Program
{
    static void Main(string[] args)
    {
        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());
        }
    }
}

这是输出结果:

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

23

使用 json 路径和 JToken 上的 SelectTokens 方法非常简单。此方法非常棒,支持通配符,例如以下内容:

jObject.SelectTokens("routes[*].legs[*].*.text")

请查看此示例代码:

private class Program
{
    public static void Main(string[] args)
    {
        string json = GetJson();
        JObject jObject = JObject.Parse(json);

        foreach (JToken token in jObject.SelectTokens("routes[*].legs[*].*.text"))
        {
            Console.WriteLine(token.Path + ": " + token);
        }
    }

    private static string GetJson()
    {
        return @" {
        ""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
                    }
                }
            ]
        }]}";
    }
}

这是输出结果:

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

3
我之前不知道 * 通配符!现在我正在尝试使用它。这可以节省很多时间... - Anthony Mason

9

如果您想获取属性的所有值,而不管它们出现在哪里,这里有一种替代递归的方法,如@brian-rogers所述,使用@mhand建议的SelectToken:

要获取duration.text的所有值,可以使用SelectToken和Linq:

var list = jObject.SelectTokens("$..duration.text")
           .Select(t => t.Value<string>())
           .ToList();

更多信息: 使用SelectToken查询JSON


1
谢谢!我没想到你可以省略成员 - 这比通配符的使用还要酷炫! - mhand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接