1 回答
TA贡献1770条经验 获得超3个赞
尝试使用父对象名称和逻辑运算符AND。结果路径将是
$..[?((@.resource.id == 5756) && (@.role.name == 'Edit'))].
它在单元测试中对我有用com.jayway.jsonpath:json-path:2.4.0
@Test
public void testParse() {
String json = "{\n" +
" \"dataObject\": [\n" +
" {\n" +
" \"id\": 263626,\n" +
" \"role\": {\n" +
" \"id\": 12054,\n" +
" \"name\": \"Edit\",\n" +
" \"description\": \"\"\n" +
" },\n" +
" \"resource\": {\n" +
" \"id\": 5756,\n" +
" \"type\": \"resource\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": 263364,\n" +
" \"role\": {\n" +
" \"id\": 12054,\n" +
" \"name\": \"Edit\",\n" +
" \"description\": \"\"\n" +
" },\n" +
" \"resource\": {\n" +
" \"id\": 5728,\n" +
" \"type\": \"resource\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
DocumentContext parse = JsonPath.parse(json);
Object read = parse.read("$..[?((@.resource.id == 5756) && (@.role.name == 'Edit'))]");
assertNotNull(read);
}
添加回答
举报