如何将 json 数据数组插入现有的 json 字段?
How to insert the json data Array into existing json field?
我有以下 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库
试试下面的代码,它使用 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);
我有以下 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库
试试下面的代码,它使用 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);