简体   繁体   English

JsonPath:按数组中的值过滤

[英]JsonPath : filter by value in array

I'm trying to filter by value an array in my Json with Jsonpath.我正在尝试使用 Jsonpath 按值过滤我的 Json 中的数组。 I want to get the long_name of the country in the JSON below.我想在下面的 JSON 中获取国家/地区的 long_name。 In order to do that, I filter the adress_components by types[0] == "country" but it doesn't seem to work.为了做到这一点,我按类型 [0] == "country" 过滤了 adress_components,但它似乎不起作用。

The JsonPath I tried :我试过的 JsonPath :

$.results[0].address_components[?(@['types'][0]=="country")].long_name

The result I want is : "Canada".我想要的结果是:“加拿大”。

The JSON : JSON:

{
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "5510-5520",
                   "short_name" : "5510-5520",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Yonge Street",
                   "short_name" : "Yonge St",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Willowdale",
                   "short_name" : "Willowdale",
                   "types" : [ "neighborhood", "political" ]
                },
                {
                   "long_name" : "North York",
                   "short_name" : "North York",
                   "types" : [ "political", "sublocality", "sublocality_level_1" ]
                },
                {
                   "long_name" : "Toronto",
                   "short_name" : "Toronto",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Toronto Division",
                   "short_name" : "Toronto Division",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Ontario",
                   "short_name" : "ON",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "Canada",
                   "short_name" : "CA",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "M2N 5S3",
                   "short_name" : "M2N 5S3",
                   "types" : [ "postal_code" ]
                }
             ]
            }
       ],
       "status" : "OK"
}

Thank you for your help.谢谢您的帮助。

The following JSONPath will work:以下 JSONPath 将起作用:

$..address_components[?(@.types[0] == 'country')].long_name

Breaking it down:分解它:

  • $..address_components : focus on the address_components array $..address_components : 关注address_components数组
  • [?(@.types[0] == 'country')] : find the address_components sub document having a type attribute named "type" containing an array of which the first value is "country" [?(@.types[0] == 'country')] :找到具有名为“type”的类型属性的address_components子文档,其中包含第一个值为“country”的数组
  • .long_name : return the long_name attribute of this sub document. .long_name :返回此子文档的long_name属性。

Verified using the Jayway JsonPath Evaluator and in Java:使用Jayway JsonPath Evaluator和 Java 验证:

JSONArray country = JsonPath.parse(json)
    .read("$..address_components[?(@.types[0] == 'country')].long_name");

// prints Canada
System.out.println(country.get(0));

The working solution offered by glytching won't anymore if country is not the first one of the types array.如果country不是类型数组中的第一个,则 glytching 提供的工作解决方案将不再适用。

You should rather use:你应该使用:

$..address_components[?(@.types.indexOf('country') != -1)]

It will filter by array contains country , rather than array starts with country它将按包含国家/地区数组过滤,而不是以国家/地区开头的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM