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

问题描述 投票:0回答:1

我们使用Rest assured自动化REST APIs,其中request json中的一些属性值需要在运行时被替换,在某些情况下可以是String,在其他情况下可以是Integer。

请求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    
  }
  }]      
} 

使用代码来替换String: "dummyvalue"


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

上一篇:成功工作。替换为Test123的值。

同样在同一个json中需要用Integer代替value:xyz value 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 payload期望值是Integer 50。如果尝试如下。

int amtIntValue = Integer.parseInt(50);

Then request.replace won't work

请指导.附上了尝试的代码.NullPointerException

Eductoin class

rest-assured rest-assured-jsonpath
1个回答
2
投票

你可以使用JsonObject在 gson;

// 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);

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

© www.soinside.com 2019 - 2024. All rights reserved.