简体   繁体   English

从管道groovy脚本中的json字符串中获取值

[英]Get the values from json string in pipeline groovy script

I had json string value. 我有json字符串值。 I need to get the values from the json string. 我需要从json字符串中获取值。 I have to try to convert the json string to json object and then get the value. 我必须尝试将json字符串转换为json对象,然后获取值。

I have try the below steps to convert the json string into json object. 我尝试了以下步骤将json字符串转换为json对象。

import groovy.js
def slurper = new JsonSlurper().parseText(str).on.JsonSlurper

But I have facing below error while get the values. 但是我在获取值时面临下面的错误。

java.lang.IllegalArgumentException: Could not instantiate {message=org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field java.lang.String id} for EchoStep(message: String): java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException. java.lang.IllegalArgumentException:无法实例化EchoStep的{message = org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:未分类的字段java.lang.String id}(消息:字符串):java.lang.ClassCastException:org.jenkinsci .plugins.workflow.steps.EchoStep.message期望使用类java.lang.String,但是收到了org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException类。

Kindly give a better suggestion to get the values from json string in groovy 请给一个更好的建议,从groovy中的json字符串中获取值

Note:I didn't restrict script approval. 注意:我没有限制脚本批准。

First, I find that Jenkins plays better with JsonSlurperClassic. 首先,我发现Jenkins在JsonSlurperClassic中的表现更好。

def slurper = new JsonSlurper().parseText(str) is all you need to make the json object, which is an odd mappy-listy object. def slurper = new JsonSlurper().parseText(str)是制作json对象所需的全部,json对象是一个奇怪的mappy-listy对象。 You can reference it like you would a map that had arrays in it: slurper.mapvalone[0].mapvaltwo[0] . 您可以像在其中具有数组的地图那样引用它: slurper.mapvalone[0].mapvaltwo[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM