工作需要,用Java实现loadrunner的web_saveparam函数,如下:

    package com.xuwangcheng.demo;
    
    /**
     * 关联参数
     * @author xuwangcheng14@163.com
     * @version 1.0.0
     * @description
     * @date 2020/7/5  21:16
     */
    public class RelationParam {
        /**
         * 左边界
         */
        private String lb;
        /**
         * 右边界
         */
        private String rb;
        /**
         * 取值顺序
         */
        private Integer order = 1;
        /**
         * 偏移量
         */
        private Integer offset = 0;
        /**
         * 取值长度
         */
        private Integer length;
    
        public String getLb() {
            return lb;
        }
    
        public void setLb(String lb) {
            this.lb = lb;
        }
    
        public String getRb() {
            return rb;
        }
    
        public void setRb(String rb) {
            this.rb = rb;
        }
    
        public Integer getOrder() {
            return order;
        }
    
        public void setOrder(Integer order) {
            this.order = order;
        }
    
        public Integer getOffset() {
            return offset;
        }
    
        public void setOffset(Integer offset) {
            this.offset = offset;
        }
    
        public Integer getLength() {
            return length;
        }
    
        public void setLength(Integer length) {
            this.length = length;
        }
    }
    
    package com.xuwangcheng.demo;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * @author xuwangcheng14@163.com
     * @version 1.0.0
     * @description
     * @date 2020/7/5  21:18
     */
    public class DemoTest {
        public static void main(String[] args) {
            String str = "{\"root\":{\"name\":\"xuwangcheng\",\"age\":30}}";
            RelationParam param = new RelationParam();
            param.setLb("\"age\":");
            param.setRb("}}");
    
            System.out.println(getValueByRelationKeyWord(param, str));
        }
    
        /**
         * 关联取值
         * @param param 参数
         * @param string 取值字符串
         * @return
         */
        private static String getValueByRelationKeyWord(RelationParam param, String string) {
            if (isEmpty(string) || param == null) {
                return string;
            }
    
            String result = "";
            if (isEmpty(param.getLb()) && isEmpty(param.getRb())) {
                result = string;
            } else {
                // 根据左右边界取值
                String regex = param.getLb() + "(.*?)" + param.getRb();
                Pattern pattern = Pattern.compile(regex);
                List<String> regStrs = new ArrayList<String>();
                Matcher matcher = pattern.matcher(string);
                while (matcher.find()) {
                    regStrs.add(matcher.group(1));
                }
                if (regStrs.size() < param.getOrder()) {
                    return null;
                }
    
                //根据order取值
                result = regStrs.get(param.getOrder() - 1);
    
                //根据offset取值
                if (param.getOffset() != null && param.getOffset() < result.length()) {
                    result = result.substring(param.getOffset());
                }
    
                // 根据length取值
                if (param.getLength() != null) {
                    result = result.substring(0, param.getLength());
                }
            }
    
            return result;
    
        }
    
        private static boolean isEmpty (String s){
            if (s == null || s.length() == 0) {
                return true;
            }
    
            return false;
        }
    
    }
    
    以前,我总以为自己是菜鸟,也总想着早起的鸟儿有虫吃。直到有一天我想飞,才愕然的发现自己没有翅膀和羽毛,我竟然是菜虫!早起的虫儿被鸟吃……原来,百足应该厚积薄发!