我们正在使用REST自动化Rest,在其中一些属性值需要在运行时被替换,在某些情况下可以是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
}
}]
} 用于替换字符串的代码:“虚拟值”
request.replace("dummyvalue", "Test123");O/P:工作成功。值替换为Test123
类似地,在同一个json中,需要用Integer: 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有效负载期望该值为Integer 50。如果按以下方式尝试:
int amtIntValue = Integer.parseInt(50);
Then request.replace won't work请指点。附加了尝试过的代码。


发布于 2020-06-05 15:08:12
您可以在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"},“教育”:{“educationE”:{“proCal”:555,"cutAmt":555,"totalAmt":555},"educationStr":"Mu","educationPro":"RT","rec":"N",“oriDate”:“2019-08-10”}"educationRes":{"pTest":"pTest_updated“、"rTest":"”、“sFl”:“}”、“educationRResult”:{“as”:“}”、“质量”:{“qualitiesE”:{“qualitiesE”: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}
https://stackoverflow.com/questions/62216972
复制相似问题