放心-JsonPath-我需要根据条件同时提取两个值

时间:2018-08-13 11:51:19

标签: java rest-assured web-api-testing rest-assured-jsonpath

我在使用JsonPath提取值时遇到困难。这是我的json,

    applications:[
    {
    "ab": 123,
    "isReady": true,
    "proId": 234
    },
    {
    "ab": 345,
    "isReady": false,
    "proId": 098
    },
    {
    "ab": 332,
    "isReady": true,
    "proId": 100
    }
]

我想在“ isReady”值为true时提取“ ab”和“ proId”的值。完成后,我需要使用上述值构造以下json。以下示例,

[
{  
   "ab": 332,
   "proId": 100
},
{
    "ab": 123,
    "proId": 234
}
]

我只能提取以下单个值,

List<Integer> abList = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}.ab");

List<Integer> proIdList = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}.proId");

在这种情况下,请您提供有关如何提取和构造所需结果的帮助吗?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试一下:

    ArrayList<Map<String,?>> result = jsonFindAllUserJobApplicantsResponse.get("applications.findAll{ it.isReady == true}");
    try {
        // use Jackson library to convert map to json string
        new ObjectMapper().writeValueAsString(result);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

参考:https://james-willett.com/2017/05/rest-assured-gpath-json/