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"
}
]
}
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
您应该将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);