如何在 groovy 中迭代一个复杂的 json 结构?
how to iterate a complex json structure in groovy?
我需要在我的 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
}
对于这些,我不知道如何深入字段值以获取其中的值
I need to iterate a json file within my jenkins pipeline in order to
get some values inside the "fields" value such as "System.AreaPath"
for instance
你可以这样做:
// get the JSON String from wherever you
// are getting it, hardcoded here for simplicity
String 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"
}
'''
def jsonObject = new JsonSlurper().parseText(json)
def result = jsonObject.fields.'System.AreaPath'
println result
这将打印 "sometext"
。
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 returns 可序列化 maps/arrays - 使用它来避免不可序列化的异常。
我需要在我的 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
}
对于这些,我不知道如何深入字段值以获取其中的值
I need to iterate a json file within my jenkins pipeline in order to get some values inside the "fields" value such as "System.AreaPath" for instance
你可以这样做:
// get the JSON String from wherever you
// are getting it, hardcoded here for simplicity
String 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"
}
'''
def jsonObject = new JsonSlurper().parseText(json)
def result = jsonObject.fields.'System.AreaPath'
println result
这将打印 "sometext"
。
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 returns 可序列化 maps/arrays - 使用它来避免不可序列化的异常。