尝试替换请求 json 中的整数值(对于使用 request.replace() 的字符串)

Trying to Replace Integer value in Request json (for String using request.replace())

我们正在使用 Rest assured 自动化 REST API,其中请求 json 需要在 运行 时替换某些属性值,在某些情况下它可以是字符串,在其他情况下可以是整数案例.

请求json:

{
 "bInfo":{
  "bEx":9, //Need to replace Integer value here
  "oriDate":"2020-07-08"    

},     

"education":{
  "educationE":{
      "proCal":9, //Need to replace Integer value here
      "cutAmt":250, //Need to replace Integer value here
      "totalAmt":20 //Need to replace Integer value here
  },
  "educationInfo":{
      "educationPur":"purtest", //Need to replace String value here
      "educationStr":"Mu",
      "educationPro":"RT",
      "rec":"N",
      "oriDate":"2019-08-10"
  } 
  },  
  "educationRes":{  
     "pTest": "", //Set String value here
     "rTest":"",
     "sFl":""
 },   

"educationRResult":{
   "as":""

}, 

 "qualities":[{

 "qualitiesE":{
 "gt":10, //Need to replace Integer value here
 "oValue":10,
 "pPr":10,
 "oVa":7,
 "cIn":1,
 "rRatio":2    
 }
  },
  {    
"qualitiesE":{
 "gt":1000,
 "oValue":10,
 "pPr":2,
 "oVa":5,
 "cIn":200,
 "rRatio":1
 }
  },
  {
  "qualitiesE":{
 "gt":70,
 "oValue":25,
 "pPr":100,
 "oVa":7,
 "cIn":40,
 "rRatio":5    
  }
  }]      
} 

用于替换字符串的代码:"dummyvalue"


request.replace("dummyvalue", "Test123");

O/P: 工作成功。值替换为 Test123

同样在同一个json需要用整数替换值:xyz值50

request.replace works only with String, if want to replace as Integer value tried

Declared String value = "50" as String and Tried

然后它将字符串值替换为“50”,因此发布时请求失败,因为 json 有效负载期望值为整数 50。如果尝试如下:

int amtIntValue = Integer.parseInt(50);

Then request.replace won't work

请指导。 附上试过的代码。

您可以使用 gson 中的 JsonObject 来实现;

// Required imports
import com.google.gson.Gson;
import com.google.gson.JsonObject;

    Gson gson = new Gson();

    // request is the json in the OP converted to a String
    JsonObject jsonObject = gson.fromJson(request, JsonObject.class);

    // Update bEx in bInfo
    jsonObject.getAsJsonObject("bInfo").add("bEx", gson.toJsonTree(555));

    // Update proCal,cutAmt and totalAmt in educationE
    jsonObject.getAsJsonObject("education").getAsJsonObject("educationE").add("proCal", gson.toJsonTree(555));
    jsonObject.getAsJsonObject("education").getAsJsonObject("educationE").add("cutAmt", gson.toJsonTree(555));
    jsonObject.getAsJsonObject("education").getAsJsonObject("educationE").add("totalAmt", gson.toJsonTree(555));

    // Update educationPur in educationInfo
    jsonObject.getAsJsonObject("education").getAsJsonObject("educationInfo").add("educationPur", gson.toJsonTree("educationPur_updated"));

    // Update pTest in educationRes
    jsonObject.getAsJsonObject("educationRes").add("pTest", gson.toJsonTree("pTest_updated"));

    // Update gt in the first item of the qualities JsonArray
    JsonObject qualitiesE = jsonObject.getAsJsonArray("qualities").get(0).getAsJsonObject();
    qualitiesE.get("qualitiesE").getAsJsonObject().add("gt", gson.toJsonTree(555));

    // Convert JsonObject back to String
    request = gson.toJson(jsonObject);

    System.out.println(request);

Output; {"bInfo":{"bEx":555,"oriDate":"2020-07-08"},"education":{"educationE":{"proCal":555,"cutAmt":555,"totalAmt":555},"educationInfo":{"educationPur":"educationPur_updated","educationStr":"Mu","educationPro":"RT","rec":"N","oriDate":"2019-08-10"}},"educationRes":{"pTest":"pTest_updated","rTest":"","sFl":""},"educationRResult":{"as":""},"qualities":[{"qualitiesE":{"gt":555,"oValue":10,"pPr":10,"oVa":7,"cIn":1,"rRatio":2}},{"qualitiesE":{"gt":1000,"oValue":10,"pPr":2,"oVa":5,"cIn":200,"rRatio":1}},{"qualitiesE":{"gt":70,"oValue":25,"pPr":100,"oVa":7,"cIn":40,"rRatio":5}}]}