1.字符串拼接
def ids = [21321,3445,3456];
def size=ids.size();
vars.put("skuNum",size);
logs.add('sku数量:'+size);
StringBuffer sb = new StringBuffer();
def random = new java.util.Random();
for (int i = 0; i < size; i++) {
def skuId = ids[i]
binding.setVariable("skuId"+i, skuId);
def count = random.nextInt(6)+2;//下单时每个skuID随机赋值一个下单数量
def subOrderId = random.nextInt(999999)+1;
vars.put("cnt",count);
binding.setVariable("count"+i, count);
logs.add('skuID:'+binding.getVariable("skuId"+i)+" 数量:"+binding.getVariable("count"+i));
sb.append("{\"skuId\":"+binding.getVariable("skuId"+i)+",\"productName\":\"goapi测试\",\"saleCount\":"+binding.getVariable("count"+i)+",\"originPrice\":0.02,\"subtotalAmount\":0.04,\"couponTotalAmount\":0.01,\"activityTotalAmount\":0.01,\"outSkuld\":\""+binding.getVariable("skuId"+i)+"\",\"subOrderld\":\""+subOrderId+"\"},");
}
def orderSkus=sb.toString().substring(0, sb.toString().length() - 1);
vars.put("orderSkus",orderSkus);
logs.add('拼接后的字符串:'+orderSkus);
if(${channelId} == 6801669){
def dateNow=${dateNow}+"A";
vars.put("dateNow",dateNow);
logs.add('抖音订单号:'+dateNow);
}
2.json 拼接
import groovy.json.JsonSlurper
def ReturnAddressDTO = runVars.get('ReturnAddressDTO');//获取上游的json对象,此时拿到的是String
def jsonObject = new JsonSlurper().parseText(ReturnAddressDTO)//转换为 jsonObject
jsonObject.put("policyTimeInDay",30);
jsonObject.put("policyType",1);
vars.put("ReturnAddressDTO",jsonObject);
标签:groovy,skuId,vars,binding,count,json,拼接,put,def From: https://www.cnblogs.com/JojoMiss/p/18108192