无方法签名:org.apache.jmeter.threads.JMeterVariables.put() 适用于参数类型:(java.lang.String, java.lang.Integer)

No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)

我有一个示例 JSON 响应如下:

{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
    "code": "color",
    "label": "Color",
    "extra_info": "",
    "values": [{
        "label": "Red",
        "value": "8"
    }, {
        "label": "Yellow",
        "value": "9"
    }, {
        "label": "Pink",
        "value": "10"
    }]
  }, {
    "code": "size",
    "label": "Size",
    "extra_info": "",
    "values": [{
        "label": "Small",
        "value": "4"
    }, {
        "label": "Medium",
        "value": "5"
    }, {
        "label": "Large",
        "value": "6"
    }]
}],
"options_available": [{
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Red"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Yellow"
    }, {
        "code": "size",
        "value": "Large"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Small"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Medium"
    }]
}, {
    "combination": [{
        "code": "color",
        "value": "Pink"
    }, {
        "code": "size",
        "value": "Large"
    }]
}],
"custom_options": []
}

我的采样器是

import org.json.JSONObject;
import org.json.JSONArray;

String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);

但是当 运行 脚本出现错误时: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)

此外还有值 (Counts=2)。我的目的是获取 "Options" 键中的数组计数(参见上面的回复)

vars.put 不支持与常规 String 不同的值,您正在尝试输入 Integer 值。

简单转换的简单出路:

vars.put("counts", Integer.toString(count));

另一个选项是使用 vars.putObject

原样保存对象
vars.putObject("counts", count);

根据 JMeter,也移动到 JSR223 采样器 best practices:

Since JMeter 3.1, we advise switching from BeanShell to JSR223 Test Elements

选择 JSR223 PostProcessor 和 Groovy 语言:

  1. Groovy performance is much better comparing to Beanshell
  2. Groovy has built-in JSON support
  3. Groovy provides a lot of enhancements on top of Java SDK

Groovy 相当于你的 7 行 Beanshell 代码是:

vars.put('counts',new groovy.json.JsonSlurper().parse(prev.getResponseData()).options.size() as String)