JMeter groovy:如何使用以数字开头的属性从 json 获取属性值

JMeter groovy: how to get attributes value from json with attributes starting with a number

我确实有以下字符串,我用 JsonSlurper() 将其解析为 json。

{"id":"1111","fields":{"2_attribute":"111111","3_attribute":"11122222","4_attribute":"1111222211"}

我在 JSR223 预处理器 groovy 脚本中使用以下代码 log.info("attribute2 :" + json2.fields.2_attribute); 获取属性“2_attribute”值并出现以下异常:

2022-06-01 13:18:25,491 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor2
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script197.groovy: 24: Number ending with underscores is invalid @ line 24, column 50 @ line 24, column 50.
   age_type_id:" + json2.fields.2_attribute

我怎样才能获得价值? 属性命名不能更改。

谢谢。

我认为您需要用引号将此 2_attribute 括起来,因为 Groovy 对象名称不能以数字开头。

def theValueIamLookingFor = new groovy.json.JsonSlurper().parse(prev.getResponseData()).fields."2_attribute"

演示:

更多信息: