json之如何使用放心在正文中发送 JsonObject 以进行发布请求
虾米姐
阅读:20
2025-01-19 22:14:33
评论:0
我有一个使用 Google Gson 创建的 JsonObject。
JsonObject jsonObj = gson.fromJson(response1_json, JsonElement.class).getAsJsonObject();
我还对现有的 jsonobj 进行了一些修改,如下所示:
JsonObject newObject = new JsonObject();
newObject.addProperty("age", "50");
newObject.addProperty("name", "X");
jsonObj.get("data").getAsJsonArray().add(newObject);
现在,使用放心,我需要使用此 jsonobject 发送 post 请求。我尝试了以下但它不起作用并抛出异常:
Response postResponse =
given()
.cookie(apiTestSessionID)
.header("Content-Type", "application/json")
.body(jsonObj.getAsString())
.when()
.post("/post/Config");
请指导我。
请您参考如下方法:
尝试使用下面的代码使用 Rest-assured 将 Json 发送到 Post 请求
//Get jsonObject from response
JsonObject jsonObj = gson.fromJson(response1_json, JsonElement.class).getAsJsonObject();
//Create new jsonObject and add some properties
JsonObject newObject = new JsonObject();
newObject.addProperty("age", "50");
newObject.addProperty("name", "X");
//Get jsonarray from jsonObject
JsonArray jArr = jsonObj.get("data").getAsJsonArray();
//Add new Object to array
jArr.add(newObject);
//Update new array back to jsonObject
jsonObj.add("data", jArr);
Response postResponse =
given()
.cookie(apiTestSessionID)
.contentType("application/json")
.body(jsonObj.toString())
.when()
.post("/post/Config");
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。