【问题标题】:Trying to Replace Integer value in Request json (for String using request.replace())尝试替换请求 json 中的整数值(对于使用 request.replace() 的字符串)
【发布时间】:2020-06-05 13:53:45
【问题描述】:

我们正在使用 Rest Assure 自动化 REST API,其中请求 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    
  }
  }]      
} 

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


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

O/P:成功运行。值替换为 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 有效负载期望该值为整数 50。如果尝试如下:

int amtIntValue = Integer.parseInt(50);

Then request.replace won't work

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

【问题讨论】:

    标签: json rest-assured


    【解决方案1】:

    您可以在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);
    

    输出; {"bInfo":{"bEx":555,"oriDate":"2020-07-08"},"education":{"educationE":{"proCal":555,"cutAmt":555,"totalAmt" :555},"educationInfo":{"educationPur":"educationPur_updated","educationStr":"Mu","educationPro":"RT","re​​c":"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}}]}

    【讨论】:

    • 感谢您的宝贵建议。我已经更新了 json 有效载荷,请看一下。它实际上是嵌套在 JSONObject 中的/JSONArray。我错过了这个节点来添加到我的问题中。请指导使用给定代码后无法解决我的问题(可能是由于 Json 错误的有效负载层次结构)
    • 我根据新的json更新了答案。您究竟想在哪里更新整数?为什么要用整数更新字符串?
    • 您可以使用这个建议的答案用整数更新InfoDetails 的值(尽管现有值的数据类型是字符串或整数)。关于您在 cmets 中提到的错误(值变为 0 代替 50);不检查代码我无法找出原因。
    • @la1 - 你能用你的方法更新 OP,以便其他成员可以帮助你。
    • 您的 json 再次无效。 (不是因为 cmets)。请添加一个可以解析的有效 json。在您提出问题之前,请使用在线 json 验证器(例如:-jsonformatter.com)来检查 json 的有效性。
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多