【问题标题】:Rest Assured - JsonPath - I need two values to extract at the same time based on condition放心 - JsonPath - 我需要根据条件同时提取两个值
【发布时间】:2018-08-13 11:51:19
【问题描述】:

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

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

当“isReady”值为真时,我想提取“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");

您能否在这个场景中帮助您了解如何提取和构建所需的结果?

非常感谢您的帮助。

【问题讨论】:

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


【解决方案1】:

试试这个:

    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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多