I've read the documentation, and I can't figure out how I perform a sub-filter using JsonPath. Here's an example...
我已经阅读了文档,我无法弄清楚如何使用JsonPath执行子过滤器。这是一个例子......
Given the following JSON, how do I retrieve a list of people with an address in Sydney
?
鉴于以下JSON,如何在悉尼检索具有地址的人员列表?
I've tried variations of $.people[?(@.addresses[*].city=="Sydney")]
to no avail. e.g.
我尝试过各种各样的$ .people [?(@ .address [*]。city ==“Sydney”)]无济于事。例如
$.people[?(@.addresses[*].city=="Sydney")]
$.people[?(@.addresses.city=="Sydney")]
$.people[?(@..city=="Sydney")]
JSON
{
"people": [{
"name": "George",
"addresses": [{
"city": "Sydney"
}]
},
{
"name": "Jane",
"addresses": [{
"city": "Brisbane"
}]
},
{
"name": "Fred",
"addresses": [{
"city": "Perth"
},
{
"city": "Sydney"
}
]
}
]
}
Preferred Output
{
"people": [{
"name": "George",
"addresses": [{
"city": "Sydney"
}]
},
{
"name": "Fred",
"addresses": [{
"city": "Perth"
},
{
"city": "Sydney"
}
]
}
]
}
I'm using the following Java library: com.jayway.jsonpath:json-path:2.2.0
我正在使用以下Java库:com.jayway.jsonpath:json-path:2.2.0
I'm using http://jsonpath.com/ to test my jsonpath in the browser (maybe that's the problem?).
我正在使用http://jsonpath.com/在浏览器中测试我的jsonpath(也许这就是问题?)。
1 个解决方案
#1
2
The problem is that @.addresses[*].city
is a collection. It is not ANY city of the person and it is not ONLY(each) city of the person.
问题是@ .addresses [*]。city是一个集合。它不是该人的任何城市,也不是该人的唯一城市。
If you are looking for persons with some addresses in Sydney the query should be:
如果您正在寻找在悉尼有一些地址的人,那么查询应该是:
"$.people[?(\"Sydney\" in @.addresses[*].city)]"
“$ .people [?(在@ .addresses [*]。city中的”悉尼“]]”
Example:
String filter = "$.people[?(\"Sydney\" in @.addresses[*].city)]";
ReadContext ctx = JsonPath.parse(json);
List<Map<String, Object>> rez = ctx.read(filter, List.class);
If you are looking for persons with all addresses in Sydney I do not know correct query. It is possible to use Predicate, but it is very complicated code.
如果您正在寻找悉尼所有地址的人,我不知道正确的查询。可以使用Predicate,但代码非常复杂。
Just for information:
仅供参考:
List<Map<String, Object>> rez = JsonPath.parse(json).read(
"$.people[?]",
List.class,
new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
Map<String, Object> map = ctx.item(Map.class);
ReadContext readContext = JsonPath.parse(JSONObject.toJSONString(map));
List<String> allAddresses = readContext.read("$.addresses[*].city");
List<String> sydneyAddresses = readContext.read("$.addresses[?(@.city=='Sydney')].city");
return allAddresses.size() == sydneyAddresses.size();
}
});
#1
2
The problem is that @.addresses[*].city
is a collection. It is not ANY city of the person and it is not ONLY(each) city of the person.
问题是@ .addresses [*]。city是一个集合。它不是该人的任何城市,也不是该人的唯一城市。
If you are looking for persons with some addresses in Sydney the query should be:
如果您正在寻找在悉尼有一些地址的人,那么查询应该是:
"$.people[?(\"Sydney\" in @.addresses[*].city)]"
“$ .people [?(在@ .addresses [*]。city中的”悉尼“]]”
Example:
String filter = "$.people[?(\"Sydney\" in @.addresses[*].city)]";
ReadContext ctx = JsonPath.parse(json);
List<Map<String, Object>> rez = ctx.read(filter, List.class);
If you are looking for persons with all addresses in Sydney I do not know correct query. It is possible to use Predicate, but it is very complicated code.
如果您正在寻找悉尼所有地址的人,我不知道正确的查询。可以使用Predicate,但代码非常复杂。
Just for information:
仅供参考:
List<Map<String, Object>> rez = JsonPath.parse(json).read(
"$.people[?]",
List.class,
new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
Map<String, Object> map = ctx.item(Map.class);
ReadContext readContext = JsonPath.parse(JSONObject.toJSONString(map));
List<String> allAddresses = readContext.read("$.addresses[*].city");
List<String> sydneyAddresses = readContext.read("$.addresses[?(@.city=='Sydney')].city");
return allAddresses.size() == sydneyAddresses.size();
}
});