gpt4 book ai didi

jenkins - 如何在groovy中迭代一个复杂的json结构?

转载 作者:行者123 更新时间:2023-12-03 18:50:34 24 4
gpt4 key购买 nike

我需要在 jenkins 管道中迭代一个 json 文件,以便在“字段”值中获取一些值,例如“System.AreaPath”,json 文件具有以下结构:

{
"fields": {
"AcornAgileProcess.BugSource": "Ad Hoc",
"AcornAgileProcess.IssueType": "Content",
"Custom.BuildVariant": "Default",
"Custom.ReproinRetail": "No",
"Custom.SQInvestigationStatus": "Not started",
"PhoenixAgileProcess.GameArea": "Art - technical/FX",
"PhoenixAgileProcess.GameMode": "In-Game (all)",
"PhoenixAgileProcess.ReproCount": 4,
"PhoenixAgileProcess.ReproRate": "5 - Consistently (100%)",
"PhoenixAgileProcess.ReproTime": 5,
"PhoenixAgileProcess.SkuTested": "sometext",
"System.AreaId": 1098,
"System.AreaLevel1": "sometext",
"System.AreaPath": "sometext",
"System.AssignedTo": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NmE1MWI2NjMtOWU2Ny03NWYyLTlkOGEtNjMwMDFhMzVlZDI3",
"displayName": "Triage",
"id": "sometext",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedAs": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedDate": "2021-03-31T23:18:58.513Z",
"System.ChangedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.ChangedDate": "2021-03-31T23:18:58.513Z",
"System.CommentCount": 0,
"System.CreatedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NTcyMTBjZmQtY2VlOC03NjFhLTk4YzctYTc2YjA4NDUyZDU5",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.CreatedDate": "2021-03-31T23:16:45.42Z",
"System.Id": 896092,
"System.IterationId": 1093,
"System.IterationLevel1": "sometext",
"System.IterationPath": "sometext",
"System.NodeName": "sometext",
"System.PersonId": 31919392,
"System.Reason": "New",
"System.Rev": 2,
"System.RevisedDate": "9999-01-01T00:00:00Z",
"System.State": "New",
"System.TeamProject": "sometext",
"System.Title": "sometext",
"System.Watermark": 1589040,
"System.WorkItemType": "Bug"
},
"id": 896092,
"relations": [
{
"attributes": {
"isLocked": false,
"name": "Related"
},
"rel": "System.LinkTypes.Related",
"url": "someurl"
},
{
"attributes": {
"authorizedDate": "2021-03-31T23:16:45.42Z",
"id": 12326470,
"name": "ParticleEffects.mp4",
"resourceCreatedDate": "2021-03-31T23:09:55.437Z",
"resourceModifiedDate": "2021-03-31T20:03:35.273Z",
"resourceSize": 23883076,
"revisedDate": "9999-01-01T00:00:00Z"
},
"rel": "AttachedFile",
"url": "someurl"
}
],
"rev": 2,
"url": "someurl"
}
我的 groovy 知识非常基础,所以我尝试了以下两种方法:
def getTags(){
def json = readJSON file: "test.json"
def tags = ""
def slurped = new JsonSlurper().parseText(json)

slurped.each{ key, value ->
value.each {k, v ->
println "${v}"
}
}
}
我收到此错误 15:30:31 hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (net.sf.json.JSONObject) values: [[fields:[AcornAgileProcess.BugSource:Ad Hoc, AcornAgileProcess.IssueType:Content, ...], ...]]

def getTags(){
def json = readJSON file: "test.json"
println json
def tags = ""
for (key in json.keySet()) {
if (key == "System.Tags"){
tags = json.get(key)
}
}
println tags
}
有了这些,我不知道如何深入了解字段值以获取其中的值

最佳答案

def json = readJSON file: "test.json"
println json.fields.'System.AreaPath'
它和
def jsonString = readFile file: "test.json"
def json = new JsonSlurperClassic().parseText(jsonString)
println json.fields.'System.AreaPath'
JsonSlurperClassic 返回可序列化的映射/数组 - 使用它来避免不可序列化的异常。

关于jenkins - 如何在groovy中迭代一个复杂的json结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66969786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com