我有以下JSON:
{
"X":20,
"Y":null
}
现在,对于键Y,我需要在json数组下面插入。
{
"A":null,
"B":1,
"C":5000,
"D":0.25
}
我尝试了这个但不起作用:
String response1 =
given()
.cookie(apiTestSessionID)
//.spec(requestSpecification)
.when()
//.get("/service/bill/Config")
.get("/service/bill/Config/0001")
.asString();
JsonPath jsonCstmrConfig = new JsonPath(response);
String response2 = given()
.cookie(apiTestSessionID)
.when()
.get("/service/commoncache/card")
.asString();
JsonPath jsonSoiRateCard = new JsonPath(response2);
Map<String,String> maps = jsonCstmrConfig.getMap("data");
maps.put("X","Value");
有没有办法用提供的放心的json库来做到这一点。
答案 0 :(得分:0)
尝试下面的代码,它使用Gson库
Gson gson = new Gson();
String response1 = given()
.cookie(apiTestSessionID)
.when()
.get("/service/bill/Config/0001")
.asString();
//Converting response string to JsonObject
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject();
String response2 = given()
.cookie(apiTestSessionID)
.when()
.get("/service/commoncache/card")
.asString();
//Converting response string to JsonElement
JsonElement element = gson.fromJson (response2, JsonElement.class);
//Adding json data array to existing jsonObject
jsonObj.add("Y", element);