尝试替换请求json中的整数值(对于使用Request.Replace()的字符串)
我们正在使用REST-assured-where-request-json自动化restapi,其中一些属性值需要在运行时替换,在某些情况下可以是字符串,在另一种情况下可以是整数 请求json:尝试替换请求json中的整数值(对于使用Request.Replace()的字符串),json,rest-assured,Json,Rest Assured,我们正在使用REST-assured-where-request-json自动化restapi,其中一些属性值需要在运行时替换,在某些情况下可以是字符串,在另一种情况下可以是整数 请求json: { "bInfo":{ "bEx":9, //Need to replace Integer value here "oriDate":"2020-07-08" }, "education":{ "educationE":{ "proCal":9, //N
{
"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”
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
请导游。
已附加试用代码。
您可以在中使用JsonObject
产出;教育:{proCal:555,cutAmt:555,totalAmt:555},教育信息:{educationPur:“educationPur\u updated”,“educationStr:“Mu”,“educationPro:”RT”,“rec:”N”,“oriDate:”2019-08-10“}”,教育资源:{“pTest:”pTest:”pTest\u updated“,”st:”sfrtel:”,“educationsulture:”Mu”,“educationPro:”N”,“oriDate:”教育质量“:”[{“质量”:{“gt”:555,“oValue”:10,“pPr”:10,“oVa”:7,“cIn”:1,“rRatio”:2},{“质量”:{“gt”:1000,“oValue”:10,“pPr”:2,“oVa”:5,“cIn”:200,“rRatio”:1},{“质量”:{“gt”:70,“oValue”:25,“pPr”:100,“oVa”:7,“cIn”:40,“rRatio”:5}}
您可以在中使用JsonObject来完成
输出;{“bInfo”:{“bEx”:555,“oriDate”:“2020-07-08”},“教育”:{“educationE”:{“proCal”:555,“cutAmt”:555,“educationfo”:{“educationPur”:“educationPur\u updated”,“educationStr”:“Mu”,“educationPro”:“RT”,“rec”:“N”,“oriDate”:“2019-08-10”},“educationRes”:{“pTest”:“pTest”:“pTest\u updated”,“rTest”:“sFl”:“educationresult”:“educations”{质量:{“质量”:{“gt”:555,“oValue”:10,“pPr”:10,“oVa”:7,“cIn”:1,“rRatio”:2},{“质量”:{“gt”:1000,“oValue”:10,“pPr”:2,“oVa”:5,“cIn”:200,“rRatio”:1},{“质量”:{“gt 70,“oValue”:25,“pPr”:100,“oVa”:7,“cIn”:40,“rRatio”:5}
感谢您的宝贵建议。我已经更新了json负载,请看一看。它实际上是JSONObject中嵌套的/JSONArray。我错过了添加到问题中的这个节点。请指导,因为在使用给定代码后无法解决我的问题(可能是由于json错误的负载层次结构)我根据新的json更新了答案。您想在哪里更新整数?为什么要用整数更新字符串?您可以用整数更新
InfoDetails
的值(尽管现有值的数据类型是String或Integer)使用此建议答案。关于您在注释中提到的错误(值为0而不是50);我无法在不检查代码的情况下找出原因。@la1-您可以用您的方法更新OP,以便其他成员可以帮助您。您的json再次无效。(不是因为注释)。请添加可解析的有效json。在提出问题之前,请使用联机json验证程序(例如:-)检查json的有效性。感谢您的宝贵建议。我已经更新了json负载,请看一看。它实际上是JSONObject中嵌套的/JSONArray。我错过了添加到问题中的这个节点。请指导,因为在使用给定的代码后无法解决我的问题(可能是由于json错误的负载层次结构)我根据新的json更新了答案。您想在哪里更新整数?为什么要用整数更新字符串?您可以用整数更新InfoDetails
的值(尽管现有值的数据类型是String或Integer)使用此建议答案。关于您在注释中提到的错误(值为0而不是50);我无法在不检查代码的情况下找出原因。@la1-您可以用您的方法更新OP,以便其他成员可以帮助您。您的json再次无效。(不是因为注释)。请添加一个可以解析的有效json。在提出问题之前,请使用联机json验证程序(例如:-)检查json的有效性。
int amtIntValue = Integer.parseInt(50);
Then request.replace won't work
// 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);