我们需要根据用户选择的过滤器将jsonpath发送到客户端计算机。
在下面的json中,我们有国家代码为IN和ENG的数据,我需要创建Jsonpath来获取国家代码为“IN”或“ENG”的记录,并且建筑物具有房间1或2。
{"国家":[{"国家":{"国家代码":"IN","建筑":[{"名称":"巴贝奇","楼层":[{"级别":1,"公寓" :[{"房间":2},{"房间":3}],"电梯":"工作"},{"楼层":2,"公寓":[{"房间":2},{"房间":3}],"电梯":"工作"},{"级别":2,"公寓":[{"房间":1},{"房间":4}],"电梯":"不工作"}]},{"name":"查尔斯","floor":[{"level":1,"lift":"notinstalled","apartment":[{"rooms":1},{"房间":2}]},{"level":2,"lift":"工作","公寓":[{"rooms":1},{"rooms":4}]}]}]}} ,{"country":{"countryCode":"ENG","building":[{"name":"Babbage","floor":[{"level":1,"apartment":[{"rooms" :2},{"房间":3}],"电梯":"工作"},{"级别":2,"公寓":[{"房间":2},{"房间":3}] ,"lift":"工作中"},{"level":2,"公寓":[{"rooms":1},{"rooms":4}],"lift":"notworking"}]}, {“名称”:“查尔斯”,“楼层”:[{“级别”:1,“电梯”:“未安装”,“公寓”:[{“房间”:1},{“房间”:2}] },{"level":2,"lift":"工作","公寓":[{"rooms":1},{"rooms":4}]}]}]}}]}
我尝试使用硬编码的 Jsonpath 过滤器,它工作正常。但我想使用 java 生成该字符串。
而不是将其硬编码为 字符串过滤器 = "$.building[*].floor[?(@.lift=="working")].apartment[?(@.rooms==1)]";
我想构建这个过滤器。
请帮忙。
你可以这样做:
public static String generateFilter(boolean includeIN, boolean includeENG, boolean includeRooms1, boolean includeRooms2) {
Filter filter = Filter.filter(Criteria.where("country.countryCode").in("IN", "ENG")
.and("building[*].floor[?(@.lift=='working')].apartment[?(@.rooms in [1, 2])]"));
String jsonPathFilter = "$[?(@.countries[" + (includeIN ? "0" : "") + (includeENG ? ",1" : "") + "])]." +
filter.toString();
return jsonPathFilter;
}