我这里有一个java代码。我在json中有3条记录。我想要一个jsonpath来匹配第一条记录(id=1)。但是如果数组在field1(id=1)下,它将不匹配。但是如果一个对象在field1(id=3)下,它可以匹配。我的问题是,是否有任何方法可以从我的代码中匹配id=1?谢谢。
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import java.util.*;
import static com.jayway.jsonpath.Criteria.where;
public class Test {
public static void main(String[] args) {
List valueAsList = new ArrayList<>();
Map map1 = new LinkedHashMap();
map1.put("field1",List.of(Map.of("a", "match","id","1")));
valueAsList.add(map1);
map1 = new LinkedHashMap();
map1.put("field1",List.of(Map.of("a", "notMatch","id","2")));
valueAsList.add(map1);
map1 = new LinkedHashMap();
map1.put("field1",Map.of("a", "match","id","3"));
valueAsList.add(map1);
Object ret = null;
ret = JsonPath.parse(valueAsList).read("$[?]", Filter.filter(where("field1[*].a").eq("match")));
System.out.println(ret);
ret = JsonPath.parse(valueAsList).read("$[?]", Filter.filter(where("field1.a").eq("match")));
System.out.println(ret);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!