首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试替换请求json中的整数值(用于使用request.replace()的字符串)

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

Stack Overflow用户
提问于 2020-06-05 13:53:45
回答 1查看 706关注 0票数 0

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

请求json:

代码语言:javascript
复制
{
 "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    
  }
  }]      
} 

用于替换字符串的代码:“虚拟值”

代码语言:javascript
复制
request.replace("dummyvalue", "Test123");

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

类似地,在同一个json中,需要用Integer: xyz值50替换值。

代码语言:javascript
复制
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。如果按以下方式尝试:

代码语言:javascript
复制
int amtIntValue = Integer.parseInt(50);

Then request.replace won't work

请指点。附加了尝试过的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-05 15:08:12

您可以在JsonObject中使用葛森

代码语言:javascript
复制
// Required imports
import com.google.gson.Gson;
import com.google.gson.JsonObject;
代码语言:javascript
复制
    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}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62216972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档