2017-03-08 125 views
0

我有以下的JSON:如何將json數據數組插入現有的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

嘗試下面的代碼,它使用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); 
+0

由於它的工作原理。 – ButterSkotch

+0

@RajanVerma很高興它幫助你... – Uttam

+0

嗨烏坦,我面臨的問題更少,你能否也請幫忙。 http://stackoverflow.com/q/42691109/7640781 – ButterSkotch