gpt4 book ai didi

java - 在 Java 和 Rest Assured 中使用嵌套标签测试 post 方法时遇到问题

转载 作者:行者123 更新时间:2023-12-02 11:08:36 26 4
gpt4 key购买 nike

RequestSpecification request = RestAssured.given().header("Content-Type", "application/json");
JSONObject requestParams = new JSONObject();
JSONObject childObject1= new JSONObject();
JSONObject childObject2 = new JSONObject();
requestParams.put("contactClass", "ZWSS");
requestParams.put("contactActivity", "0039");
requestParams.put("contractAccountNumber", "210024144291");
requestParams.put("text", "Please rate the overall ease of using the website to initiate or make your service request");
requestParams.put("contactType", "Z1");
requestParams.put("contactDirection", "1");
childObject1.put("question", "0001");
childObject1.put("answer", "01");
childObject1.put("question", "0002");
childObject1.put("answer", "02");

requestParams.put("addInfo", childObject1);
requestParams.put("addInfo", childObject2);

request.body(requestParams.toString());
Response response = request.post("https://api-dev.adp.com/api/*/*/*");

我正在尝试在 Restassured 框架中使用嵌套标签测试 Post 方法,上面是我在 JSON 对象中构建请求的代码片段。有些我的 JSON 没有同时保存 Addinfo 的值(问题和答案),并且请求失败,并且请求采用以下格式。

{"contractAccountNumber":"210024144291",
"contactClass":"ZWSS",
"contactActivity":"0039",
"contactType":"Z1",
"addInfo":{"question":"0002","answer":"02"},
"text":"Please rate the overall ease of using the website to initiate or
make your service request",
"contactDirection":"1"}

但请求应采用以下格式

 {
"contactClass": "ZWSS",
"contactActivity": "0039",
"contractAccountNumber": "210024144291",
"text": "Please rate the overall ease of using the website to initiate or
make your service request",
"contactType": "Z1",
"contactDirection": "1",
"addInfo": [{
"question": "0001",
"answer": "01"
},
{
"question": "0002",
"answer": "02"
}
]
}

任何人都可以帮助我解决这个问题..

最佳答案

您应该将 addInfo 作为数组传递

    childObject1.put("question", "0001");
childObject1.put("answer", "01");

childObject2.put("question", "0002");
childObject2.put("answer", "02");

JSONArray jsonArray = new JSONArray();

jsonArray.put(childObject1);
jsonArray.put(childObject2);

requestParams.put("addInfo", jsonArray);

关于java - 在 Java 和 Rest Assured 中使用嵌套标签测试 post 方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com