JsonPath :按数组中的值过滤
JsonPath : filter by value in array
我正在尝试按值过滤我的 Json 中具有 Json 路径的数组。我想获取下面JSON中国家的long_name。为了做到这一点,我按类型[0] == "country"过滤adress_components,但它似乎不起作用。
我试过的Json路径:
$.results[0].address_components[?(@['types'][0]=="country")].long_name
我想要的结果是:"Canada".
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"
}
感谢您的帮助。
以下 JSONPath 将起作用:
$..address_components[?(@.types[0] == 'country')].long_name
分解:
$..address_components
:关注address_components
数组
[?(@.types[0] == 'country')]
:找到 address_components
子文档,其类型属性名为 "type",包含第一个值为 "country" 的数组
.long_name
: return 该子文档的 long_name
属性。
使用 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));
如果 country 不是类型数组中的第一个,glytching 提供的工作解决方案将不再有效。
你应该使用:
$..address_components[?(@.types.indexOf('country') != -1)]
它将按包含国家/地区的数组进行过滤,而不是以国家/地区开头的数组
我正在尝试按值过滤我的 Json 中具有 Json 路径的数组。我想获取下面JSON中国家的long_name。为了做到这一点,我按类型[0] == "country"过滤adress_components,但它似乎不起作用。
我试过的Json路径:
$.results[0].address_components[?(@['types'][0]=="country")].long_name
我想要的结果是:"Canada".
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"
}
感谢您的帮助。
以下 JSONPath 将起作用:
$..address_components[?(@.types[0] == 'country')].long_name
分解:
$..address_components
:关注address_components
数组[?(@.types[0] == 'country')]
:找到address_components
子文档,其类型属性名为 "type",包含第一个值为 "country" 的数组
.long_name
: return 该子文档的long_name
属性。
使用 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));
如果 country 不是类型数组中的第一个,glytching 提供的工作解决方案将不再有效。
你应该使用:
$..address_components[?(@.types.indexOf('country') != -1)]
它将按包含国家/地区的数组进行过滤,而不是以国家/地区开头的数组