当前位置: 首页 > 知识库问答 >
问题:

JsonPath:按数组中的值筛选

夏星阑
2023-03-14

我试图通过值过滤我的Json中的一个数组与Jsonpath。我想在下面的JSON中获得国家的long_name。为了做到这一点,我通过类型[0]==“国家”过滤adress_components,但它似乎不起作用。

我试过的JsonPath:

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

我想要的结果是:“加拿大”。

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"
}

谢谢你的帮助。

共有2个答案

鲁鸿朗
2023-03-14

如果country不是数组类型中的第一个,glytching提供的有效解决方案将不再适用。

你应该使用:

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

它将筛选数组包含国家,而不是数组开始与国家

权韬
2023-03-14

以下JSONPath将起作用:

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

打破它:

  • $...address_components:关注address_components数组
  • [?( @.类型[0]=='国家')]:查找address_components子文档,该子文档具有名为type的类型属性,其中包含一个数组,其中第一个值是"国家"
  • .long_name:返回此子文档的long_name属性。

使用Jayway JsonPath评估器和Java验证:

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

// prints Canada
System.out.println(country.get(0));
 类似资料:
  • 我尝试过滤,以获得值从订阅号内*...ou.subscriber具有扩展的dinfo[SOURCE_OR_TARGET=="目标"],但我的jsonpath它不工作。 我尝试的jsonpath: 结果,我想得到这个subscriberNumber=“001” JSON: 谢谢你的帮助。

  • 我有这个json: 我想使用通过其名称查找元素“foo”(“foo”) 我尝试了类似 但它似乎不起作用(我检查了 https://jsonpath.com)

  • 我有这样的矩阵: 我需要在最后一列中过滤它tp get value 1。所以结果应该是这样的: 我的当前代码是: 我的代码返回一个空数组。我一定错过了什么...

  • 我刚刚过滤了一些数据,现在我有一个。csv文件,但我注意到我只需要选择具有最低价格的行: 例子: 在这个例子中,我只想得到第三行和第六行: 使用python,如何获得最终的表?

  • 有一个数据帧: 以及熊猫系列: 如何创建包含c1在list1中的行的新数据帧。 输出:

  • 之后,我有了一个控制器,它自动将参数绑定到谓词,如下所示: 因此,我可以使用如下所示的特定过滤器从存储库查询order: 问题是我无法过滤列表中的值。我也尝试,但得到 注意:我使用spring boot 1.3.4和querydsl-jpa 3.7.4 谢了。