0

我正在尝试使用 json 解析文本文件并从 jsonArray 中获取元素之一。下面是我试图解析的 json

[
  {
    "ContainerConfig": {
      "Labels": {
        "commit-id": "abcdef123d",
        "author": "Jon"
      }
    }
  }
]

下面是我在 jenkinsfile 中的 groovy 实现

def jsonStr=readFile('temp.txt').trim()
//here temp.txt consist of above json 

JsonSlurper slurper = new JsonSlurper()
def parsedJson=slurper.parseText(jsonStr)

def commitId=parsedJson[0].ContainerConfig.Labels.commit-id

我收到此错误消息 -

java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList 

        
4

1 回答 1

1

Using the JsonSlurper is not really best practice and can cause problems with CPS, use readJSON instead (which is also easier to use IMO).

I also suspect that the - in commit-id causes the error and you should the ["commit-id"] snytax instead.

于 2020-09-30T19:47:25.813 回答