jmeter groovy:如何从json中获得属性,属性以一个数字开头

发布于 2025-02-03 14:44:13 字数 725 浏览 1 评论 0原文

我确实有以下我的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

我如何设法获得价值? 属性命名不会改变。

谢谢。

I do have the following string which I parse with the JsonSlurper() to json.

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

I'm using in a JSR223 PreProcessor groovy script to get attribute "2_attribute" value with the following code log.info("attribute2 :" + json2.fields.2_attribute); and getting the following exception:

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

How could I manage to get the value?
attribute naming cannot change.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-02-10 14:44:13

我认为您需要将此2_Attribute带有引号,就像Groovy对象名称中的名称不能以数字开头。

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

演示:

更多信息:

I think you need to surround this 2_attribute with quotation marks as in Groovy object names cannot start with a number.

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

Demo:

enter image description here

More information:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文