请放心,如何在api的JSON响应中使用其同级属性值获取特定的属性值?

问题描述 投票:0回答:1

下面是我的回复{“ id”:123,“名称”:“ text1”},{“ id”:456,“ name”:“ text2”}]

我想找到名称为'text2'的id的值

json api rest-assured rest-assured-jsonpath
1个回答
0
投票

自从确保放心使用Groovy的GPath以来,您可以使用以下表达式:

String json = "[{ \"id\": 123, \"name\": \"text1\" }, { \"id\": 456, \"name\": \"text2\" }]";
JsonPath path = JsonPath.from(json);
System.out.println(path.get("find { it.name == 'text2' }.id"));

上面的代码将返回456

仅当JSON以数组[]开头时,它才有效”>

© www.soinside.com 2019 - 2024. All rights reserved.