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

标签 java json rest-assured

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/

相关文章:

java - iText - PDF 不好

java - Hamcrest 匹配器检查响应 json 数组中的任何元素是否具有与 Rest Assured 中的特定值相同的属性值

javascript - 循环中页面对象的范围丢失

c# - 如何使用 json 通过 SSL 在 IIS 中托管的 WCF 端点上使用基本身份验证?

java - 如何在没有字符串的情况下验证正文响应?

java - RestAssured 中出现超时错误,而服务在 postman/soapUI 中给出响应

java - 如何在 Mac 上创建一个允许用户创建目录的 JFileChooser?

java - 从哪里获取 tools.jar 以用于 Java 8 jdk 早期版本

java - 如何从加密密码中解密密码

ajax - 如何在服务器上启用跨域请求?