2017-03-08 126 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